The ezimerchant template system makes it possible to not only change the way that a site looks but also how it works. In this template example we look at how to render sub categories into the category list.
In /base.ezitpl you will find {Navigation}. This is the tag that renders the category navigation. To find out where it is defined, it helps to understand how base.ezitpl interracts with the rest of the template. You will find {Navigation} is actually defined in several files (/content.ezitpl, /products/category.ezitpl, /products/product.ezitpl to name a few). By default the definition is the same in all of them:
<h3>Categories</h3>
{Categories}
So now we need to find the definition of {Categories}. The file /include/base.ezimac includes several such declarations including Categories:
Categories:
{include file="/include/categories.ezitpl"}
Looking at /include/categories.ezitpl we see:
<ul id="categories">
{for CategorySet}
<li id="c{ID}"><a href="{URL}">{Name}</a></li>
{/for}
</ul>
To complete the picture, we need to see where the data comes from. There is a /data directory in the template. Because this is a data set that's used throughout the entire site it is defined in /data/base.ezisql. Here we find:
CategorySet:
SELECT ID, Name, '/category' || ID || '_1.htm' AS URL
FROM Category
WHERE ParentID = 0 AND Enabled != 0
ORDER BY Sequence
To output sub categories a new data set will need to be defined.
SubCategorySet:
SELECT ID, Name, '/category' || ID || '_1.htm' AS URL
FROM Category
WHERE ParentID = {ID} AND Enabled != 0
ORDER BY Sequence
Note that it is basically the same query as CategorySet except that it uses {ID} for ParentID, which will
use the ID of the category.
The last thing to do is to use the new set in /include/category.ezitpl:
<ul id="categories">
{for CategorySet}
<li id="c{ID}"><a href="{URL}">{Name}</a>
{if SubCategorySet}
<ul>
{for SubCategorySet}
<li id="c{ID}"><a href="{URL}">{Name}</a></li>
{/for}
</ul>
{/if}
</li>
{/for}
</ul>
This will conditionally output sub categories.
There is a limitation as this will only output one level of sub categories. Having said that though, this approach would not be suitable for a large category list anyway.
Comments
1 comment
this looks like a great thing to do but when I go into the directory I don't see those files in the root. only 4 directories: pages, phone, tablet and template. In template I do find categorybase.ezitpl in there but assume this isnt what is meant, it just looks like i have an entirely different directory structure.
Is it possible that these files will be created when publishing the site?
Please sign in to leave a comment.