Different templates for different categories

Freshness Warning
This article is over 6 years old. It's possible that the information you read below isn't current.

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.


Your comments:

Text only, no HTML. URLs will automatically be converted to links. Your email address is required, but it will not be displayed on the site.

Name:

Not your company or your SEO link. Comments without a real name will be deleted as spam.

Email: (not displayed)

If you don't feel comfortable giving me your real email address, don't expect me to feel comfortable publishing your comment.

Website (optional):

Follow me on Twitter

Lijit Search

Best Of

Recently Read

Get More

Subscribe | Archives

6

Recently

In which Vonage pretends their prices haven't changed (Apr 12)
Translating what Vonage marketing says about their price increase into plain English.
Twitter app competition (Apr 12)
Life as a Twitter app developer is far from over.
Twitter app competition (Apr 12)
Life as a Twitter app developer is far from over.
The rest of the world is not like you (Apr 5)
Normal people are different. Keep that in mind when creating or marketing a product.
Ideas, Risk, and Investors (Jan 1)
Over at SacStarts, I have piece up discussing a common question I get from entrepreneurs....
VoiceXML for web developers (Dec 17)
Building voice applications isn't hard at all. Any web developer can do it.
De-skunking a dog (Oct 27)
How to clean up your pet after a skunk attack.
Pressure sales via Twitter (Oct 16)
Sticking an ad in my face when we first meet is a good way to lose my interest.

Subscribe to this site's feed.

Elsewhere

IMified
Build instant messaging applications. (My company)
SacStarts
The Sacramento technology startup community.
Pinewood Freak
Pinewood Derby tips and tricks

Contact

Adam Kalsey

Mobile: 916.600.2497

Email: adam AT kalsey.com

AIM or Skype: akalsey

Resume

PGP Key

©1999-2010 Adam Kalsey.
Content management by Movable Type.