Chapter 5: Categories
Working with Category Tags
Problems
You need to display category data in your published pages.
Solutions
Use the template tags prefixed with MTCategory.
Discussion
Category tags can be used to display a list of categories and their
information in both hierarchical and non-hierarchical forms. These tags can
be used in MTArchiveList archive_type="category",
MTEntryCategories, MTCategories or
MTSubCategories tag contexts. Category tags may also be used
anywhere in a Category Archive template.
The basic tags when working with non-hierarchical data are as follows. (For an exhaustive list of Category tags see Appendix A.)
MTCategories [show_empty="1"]A container tag that loops through the list of categories in your weblog. The optional
show_emptyattribute forces a category to be displayed even if no entries have been assigned to it. Without it only categories with a count of one or more will be displayed. This list of categories is created without consideration to their hierarchy.MTCategoryLabelThe category title.
MTCategoryArchiveLinkA link to the archive page for this category. Unless category archives have been enabled, this tag may not be used and will result in an error.
MTCategoryCountThe number of entries in this category.
Here is a sample layout that produces an unordered list of category titles with their count in parenthesis.
<ul>
<MTCategories show_empty="1">.
<li><a href="<$MTCategoryArchiveLink$>">
<$MTCategoryLabel$></a> (<$MTCategoryCount$>)</li>
</MTCategories>
</ul>
The MTCategories tagset produces output for each category
without respect to its placement in the hierarchical structure.
The hierarchical structure of categories can be reflected in a template as well. Producing hierarchical category layouts requires the use of a more complex set of tags and interactions.
In MTCategories the example layout has one category where
this block is repeated for each. This behavior is similar to that of
tagsets such as MTEntries and MTComments.
However, category hierarchies where any category can have additional,
associated child-categories is not allowed. For this MT uses a technique
called recursion, where the layout is repeated inside of itself.
Here are a few of the many tags used to display and interact with these hierarchical category structures.
MTSubCategoriesA container tag similar to
MTCategoriesexcept that it respects the hierarchical structure.MTSubCatIsFirstA conditional tag that displays its contents only during the first
MTSubCategoriesloop.MTSubCatIsLastA conditional tag that displays its contents once the last
MTSubCategoriesloop has been reached.MTSubCatsRecurseThe point where a list of children should be displayed using exactly the same format defined in
MTSubCategories.
Let's look at an example. The following will produce nested, unordered lists that display the category hierarchy:
<MTSubCategories>
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<li>
<$MTCategoryLabel$>
<$MTSubCatsRecurse$>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
In this example, MTSubCategories loops through a block of
template code. When it reaches MTSubCatsRecurse the block is
then repeated for each category for which the current category is a parent.
MTSubCatIsFirst and MTSubCatIsLast are used to
output the necessary HTML tags for displaying a hierarchical list.



