FAQ: Different Templates for different Categories

Question

I have a couple of categories. I preferred if entries belonging to one category were styled in a different way than entries belonging to another category. Can I have per category templates?

Answer

Movable Type does not allow this right out of the box. However, there are a couple of ways for achieving this behavior.

You can use the Supplemental Category Tags. This is a collection of several category-related tags. Among others, the collection also contains the »MTIfCategory« and »MTIfPrimaryCategory« tags for creating a condition based on an entry's category.

On MT Extensions, Per Category Templates you can read a tip about how to use the Supplemental Category Tags.

On Elise's Learning Movable Type you will find the article Category Customizations, covering several category-related tasks. As for example, excluding certain categories, including certain categories, and showing previous / next entries within a category.

No special plugins, please

Even if you do not want to install special category-related plugins, you can do almost anything yourself with just the Compare Plugin.

If you do not know the Compare Plugin, please read Comparison needed for conditional Generation. In that article I describe this general-purpose plugin, which can be used in many different scenarios.

Suppose you want to use one template in most of the cases, but the category »FAQ« needs a special template. You will then create two module templates. For example, name them as follows.

  • module-archive-category-default
  • module-archive-category-faq

Put the specific code for the default and the FAQ category archive into these templates. Then, in the normal category archive template, simply write.

<MTIfEqual a="[MTCategoryLabel]" b="FAQ">
<MTInclude module="module-archive-category-faq">
<MTElse>
<MTInclude module="module-archive-category-default">
</MTElse>
</MTIfEqual>

So the category archive template is nothing more than a switch, including one or the other template module.

If you do not know the MTInclude tag, please read Organize your Templates with MTInclude for reading about this important Movable Type tag.

Even more complex conditions can be handled with the Compare Plugin. Suppose you have a category hierarchy. Under the FAQ category, there are several layers of categories. So, how can you find out, whether a category belongs to the FAQ sub tree? It can be done as follows.

<MTSetVar name="mgs_is_cat" value="0">

<MTParentCategories>
<MTIfEqual a="[MTCategoryLabel]" b="FAQ">
<MTSetVar name="mgs_is_cat" value="1">
</MTIfEqual>
</MTParentCategories>

<MTIfEqual a="[MTGetVar name='mgs_is_cat']" b="1">
<MTInclude module="module-archive-category-faq">
<MTElse>
<MTInclude module="module-archive-category-default">
</MTElse>
</MTIfEqual>

If you have problems reading the above lines, please have a look at Build Conditions with the Compare Plugin. In this article I describe, how to build complex conditions with the Compare Plugin.

mgs

Feedback is welcome!

What do you think about this entry? Was it interesting or boring? I would like to hear your comments. If the text was helpful, please consider setting a link to http://www.movable-type-weblog.com/.

No spam please!

For protecting this weblog I have installed the MT-Approval Plugin. You have to view a new comment in preview mode, before it is saved on the server. Moreover, I will view your comment manually, before it is published. You can find more information on the subject in the entry Weblog Spamming Basics.

With an active TypeKey session, your comment will be published immediately.

Post a new comment

TypeKey has temporarily been disabled at this location. Please create your comment without using TypeKey or log in from the preview dialog.




Remember Me?