MovableType Templates

| | Comments (5)
Last night I discovered a wonderful feature in MovableType. Template Modules. Effectively it is Server Side Includes (SSI) but integrated completely into MovableType. Strangely enough they are located at the bottom of the templates page in the MovableType admin, so I didn't even see them until just yesterday.

I found them while I was modularizing my site using SSI. I was extracting common bits and putting them in .inc files and saving them as Index templates in MT. I ran into problems though because when one of these included pages contained MT Template Tags, MT tried to process them when the .inc page was generated to disk. I didn't want them to be processed then though, I wanted them to be processed AFTER they were included. To do that I would have had to save the files to disk manually which means they wouldn't exist in the MT database. A bad thing if the directory is wiped and I need to rebuild from the MT database files.

In retrospect it was a lost cause approach from the beginning since SSI works at page delivery time and not at MT page-build time. Even if I had saved the .inc files manually, they would have been included when the page was requested and just appeared as unprocessed tags in the delivered page. So I started hunting.

Enter MT Template Modules. Essentially these are exactly what I was looking for. Unprocessed chunks of template code that can be MTInclude'ed in the standard templates wherever I need them. Heaven.

The column of stuff down the right side of the main index page are separate modules. At the time of this writing: Search, Recent Entries, Blogroll and PoweredBy. This means on each new template I can mix and match what I want to appear just by adding and removing single lines.

For example the 'links' div (the html behind the side column) in the Main Index template used to be about 30 to 40 lines of html. If I wanted to remove a section or re-arrange, it meant picking through the assortment of divs, copying and pasting and seeing how it looked. Now I just have this:

<div id="links">

    <$MTInclude module="Side Search"$>

    <$MTInclude module="Side Recent Entries"$>

    <$MTInclude module="Side Blogroll"$>

    <$MTInclude module="Side Powered"$>


Cool, huh? My main templates now are one tenth their original size and very easy to work with. The real power though then comes when I want to change something like what the 'Search' module looks like. I change it once, rebuild affected pages and I'm done.

One of the other things I made modular is the banner/header displayed at the top of each page. The banner is effectively everything inside of the coloured striped including the navigation. What I wanted to do next though was have the current page you are on somehow marked so you could see where you were. When viewing the main page, the 'main' nav text would be dark and not a link. When viewing the archives page the 'archives' nav text would be dark and not a link. Sounds easy, eh? Well it should be -- but isn't.

I first considered doing it with JavaScript, which would work, but is cumbersome and I didn't want to do the processing client-side. It's like letting a friend borrow a shirt but handing it over dirty so it needs to be washed first. It just seems rude. I like to hide the workings of how a page is produced which MT does quite well. No client-side leaves us with server-side and whatever tools MT provides.

I first checked out if there are any general built-in conditional tags I could use. No go. All the conditional container tags in MT are designed for a specific purpose. For example: <MTEntryIfExtended> and <MTEntryIfAllowComments>. Somehow this just seems wrong to me, but I persevered. Next I checked out MT's external resources page and found a list of third party plugins. One in particular caught my eye: the regex plugin by Brad Choate -- and more specifically the <MTIfMatches> tag. After downloading and installing the plugin to my remote host I started to play around with it. It works. I am able to define a variable named NavLoc at the top of each template index using <MTSetVar> and then check to see what the value is using <MTIfMatches> and <MTGetVar> inside of the banner module. Cool huh? Not so fast though. That check allowed me to display the unlinked version of the nav, but I needed an 'Else' clause. What to do in the case it doesn't match.

Well as it happens, MT v2.51 introduced an <MTElse> tag that can be applied to any MT conditional tag (or plugin) to take care of the else clause. Cool. I was running MT v2.5 and so needed to upgrade. I downloaded, ungzipped, untarred and copied across all the new files. No problems. I then started experimenting with the <MTElse> along with <MTIfMatches>. There was no joy in mudville. For whatever reason it wasn't working no matter what combination tags I tried. The <MTElse> tag was just being ignored. A read through the plugin forum on MT's site turned up the problem. The regex plugin isn't coded like a true MT conditional Template tag -- and so is immune to the suggestive pleadings of the <MTElse> tag. And also at the time of this writing, there did not exist an <MTIfNotMatches> tag. Argh. And that's where I stand. Brad Choate mentions that he'll consider adding such functionality in the next version and so in the meantime my nav links are page-oblivious -- remaining links whether they need to be or not.


MovableType's modules are one of the best and most unique features about it I think. Now, do you have any ideas about either posting entries via email or are you aware of any freebie template sets for it?

I just noticed this entry -- I'm not sure if you were aware or not, but the latest release of MT-Regex (1.61 currently) does include the MTIfNotMatches tag you were hoping for.

That is really cool (found this on google).

is there a quick way to change the css of the page without having to rebuild the entire site every time? At the moment small changes mean a slow rebuild.
Even downloading .css into BBedit, changing it, and using the built in viewer, it complains about the MT tags.
Please tell me there's a fast way to change the css?

Yes there is a way to not rebuild after simple CSS changes. If you store your CSS in a separate file and use the

   <link rel="stylesheet" href="/path/to/css/file" type="text/css" />

syntax in the HEAD section of all your pages, then your templates won't change and no rebuild will be needed.

Hope this helps.

Leave a comment

About this Entry

This page contains a single entry by Dylan published on November 8, 2002 11:37 AM.

Distributed Proofreaders was the previous entry in this blog.

Spaces Public Alpha is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.