Different templates for different categories

Freshness Warning
This blog post is over 17 years old. It's possible that the information you read below isn't current and the links no longer work.

Here’s some more cleaning out my idea database. This is yet another half-conceived idea that if you want to take it, flesh it out, and run with it, it’s yours.

I get asked on occasion how to get Movable Type to use a different templates for each category on the blog. Now that you can segment your site with subcategories, I suspect that more people will want to do this. So here’s a rough outline of how. Please don’t link to this as a tutorial. I don’t want confused MT users to have to sift through my barely coherent ramblings. But if you want to write up a tutorial on how to do this, put a link to it in the comments.

You can have as many category templates as you want. Create a different one for every category.

You can have MT create a different archive file for each template.

Instead of using the category link tags that MT provides, create your own links to the category archive file you want to use for a particular category.

So you might create a category template called news and one called products. Then you change the archive filename format in the MT preferences to output two sets of archive files. One using your news templates and one using the products template. Then in your site, you change the links from <MTArchiveLink> to a manual link like /path/to/the/news/archive/files

So your links now point to a different set of templates depending on what category you want.

KO
September 9, 2004 3:08 AM

This isn't a very elegant way of going about things - this way if you have n categories then you end up having n different templates for each category! And one of the main advantages of a cms is not having to make manual links in the first place. Ideally, you could make any number of category templates, and for each category select which template (or templates) that category should use. This doesn't seem to hard to implement either - MT already stores metadata about each category, so this is just one additional thing to store - the preferred template.

Stefan Seiz
September 9, 2004 4:45 AM

Coincidentially i was fighting with the very same problem yesterday. I ended up simply installing an MT-Plugin called "Supplemental Category Tags" . With this plug in, i can have conditional tags in my Templates which for instance restrict one template to only list a certain category or print special headers based on the category (the user clicked on). Here's a code-snippet from my CategoryArchive Template which prints a special header only for one certain category: print special header... This plugin even supports regex if the regex MT plugin is intalled. I really wonder why MovableType doesn't have any Conditional Category TAGS built in yet. Stefan

Stefan Seiz
September 9, 2004 4:48 AM

gosh, your mt ate my link to the plugin and my example code ;-) Here's the link to the plugin: http://www.stefanseiz.com/cgi-bin/mt.cgi?__mode=view&_type=template&id=11&blog_id=1 And the sample code (hopefully it works this time): <MTIfCategory name="CGVirusScan">    print special header... </MTIfCategory>

Scott Houchin
September 9, 2004 5:57 AM

I've done a couple sites with different templates for different categories, but none of the solutions were all that satisfactory. The best solution I've found was to use the IncludePlus plug-in. This plugin defines an MTIncludeModule container tag. The name of the module to include is contained in the contents of that tag. What you can then do is something like this: The only downside to this is that you either * Need to have custom templates for every category, without fail * Need to have other logic in the template to do something else for categories that don't have custom tags. What I would love to see is a "default" attribute to MTIncludeModule, which specifies the template to include (or even to just skip it) if the module specified by the contents doesn't exist.

Deane
September 10, 2004 7:31 AM

I've wanted to do this for years, but never got around to it. I was thinking that you could include another template based on the category using some kind of control structure (with a default for the "standard template"). For instance, if the category is "cars," include template module called "cars.tmpl" or something. There's got to be a plugin out there that would supply the pieces for this. On another note, it's odd that this hasn't been built into MT. Using Brad's KayValue plugin with this, would allow you to kludge your way into the "store any object" Nirvana of content management.

thomas
September 20, 2004 5:42 AM

I can't help feeling this isn't right or elegant, but what about using one nasty, complicated template file for everything, and dynamically generating a category-specific link to an extra css file to manage the look and feel of each category? the MT line would be something like: .css" type="text/css" /> so if the entry's main category is category1, this would create a link to "category1.css". This obviously doesn't help much if you assign the entry to several categories and want it treated with a different template each time you approach it from a different archive index. One possible advantage I can see is that even if the css file isn't present, nothing much goes wrong... not the case if you start to replicate template files and mis-name them, miss bits out or forget to build them. It would be so much nicer if you could select a default template for each category or entry (or both, with a level of override priority?) as you set them up in MT, of course...

Josiah Hincks
December 5, 2005 5:18 PM

I don't think there is any elegant solution if you need differential HTML markup for entries in different categories. A more modest and managable approach for static pages where you only need differential CSS might be: (1) Have a default CSS file that is always applied. (2) Have an additional category specific CSS file that a link is made to using MTIfCategory (from the Supplemental Category Tags plugin). There is no need to either create or customise the category specific CSS file unless you needed it since the default layout would be applied.

Henning von Vogelsang
June 12, 2006 3:31 AM

Applying a different CSS for each category might help to change the background color or some used images and font styles, but it doesn't change the structure, which is stored in XHTML. So if I want to actually change the design of the page (different placement of elements), not just its looks, the approach with multiple style sheets won't work. The plugin using is conditional. It works well to show/hide certain elements. A while ago I made a website using this method to display different header images per category. If you want to use the link list to all categories, automatically generated by Movable Type, Adam's solution won't work for you. However, with the MTInclude tag, you could generate a new 'automatic list' by your own, which you would have to change only in one instance, in your modules (MT 3.2).

Adam Kalsey
June 12, 2006 10:15 AM

Henning: CSS can do a LOT more than change colors and images. The entire layout can be changed using CSS. See http://www.csszengarden.com/ for some examples. All those designs use the same HTML file, just different stylesheets.

This discussion has been closed.

Recently Written

The Trap of The Sales-Led Product (Dec 10)
It’s not a winning way to build a product company.
The Hidden Cost of Custom Customer Features (Dec 7)
One-off features will cost you more than you think and make your customers unhappy.
Domain expertise in Product Management (Nov 16)
When you're hiring software product managers, hire for product management skills. Looking for domain experts will reduce the pool of people you can hire and might just be worse for your product.
Strategy Means Saying No (Oct 27)
An oft-overlooked aspect of strategy is to define what you are not doing. There are lots of adjacent problems you can attack. Strategy means defining which ones you will ignore.
Understanding vision, strategy, and execution (Oct 24)
Vision is what you're trying to do. Strategy is broad strokes on how you'll get there. Execution is the tasks you complete to complete the strategy.
How to advance your Product Market Fit KPI (Oct 21)
Finding the gaps in your product that will unlock the next round of growth.
Developer Relations as Developer Success (Oct 19)
Outreach, marketing, and developer evangelism are a part of Developer Relations. But the companies that are most successful with developers spend most of their time on something else.
Developer Experience Principle 6: Easy to Maintain (Oct 17)
Keeping your product Easy to Maintain will improve the lives of your team and your customers. It will help keep your docs up to date. Your SDKs and APIs will be released in sync. Your tooling and overall experience will shine.

Older...

What I'm Reading

Contact

Adam Kalsey

+1 916 600 2497

Resume

Public Key

© 1999-2022 Adam Kalsey.