Feature
Post

Category
Code


Anatomy of a Movable Type Template

In my last post, I talked about the different types of templates and what they’re used for. Now that we have a feel for what our templates do, let’s take a closer look at one and see what’s going on inside. This is the default Main Index template:

<MTSetVar name="page_id" value="main-index">
<MTSetVar name="main_template" value="1">
<MTSetVar name="main_index" value="1">
<MTSetVar name="sidebar" value="1">
<MTSetVar name="module_recent_posts" value="1">
<MTSetVar name="module_category_archives" value="1">
<MTSetVar name="module_author_archives" value="1">
<MTSetVar name="module_monthly_archives" value="1">

<$MTInclude module="Header"$>

<MTEntries>

<$MTEntryTrackbackData$>
    <MTDateHeader>
        <h2 class="date-header"><$MTEntryDate format="%x"$></h2>
    </MTDateHeader>
    <$MTInclude module="Entry Summary"$>
</MTEntries>

<div class="content-nav">
    <a href="<$MTLink template="archive_index"$>">Archives</a>
</div>

<$MTInclude module="Footer"$>

First, a point about MT tag syntax. If you read my last post, you might have noticed I was writing MT tags like this:

<mt:entries>

But in the template above, the same tag is written like this:

<MTEntries>

Movable Type has become pretty flexible when it comes to writing tags. Both of the previous examples work, as do the following:

<mtentries>
<$MTEntries$>

Basically, go with what you like. I prefer the <mt:entries> format, so I will typically write code in that format, but if I’m quoting from the default templates, I’ll leave it just as I found it.

Another thing you need to know is there are two types of MT tags: container tags and empty tags. Container tags will have an opening and closing tag, like so:

<mt:entries></mt:entries>

While empty tags stand alone, with an optional ending slash:

<mt:entrytitle>
<mt:entrybody/>

Now, the first few lines in the template above are all <MTSetVar> tags:

<MTSetVar name="page_id" value="main-index">
<MTSetVar name="main_template" value="1">
<MTSetVar name="main_index" value="1">
<MTSetVar name="sidebar" value="1">
<MTSetVar name="module_recent_posts" value="1">
<MTSetVar name="module_category_archives" value="1">
<MTSetVar name="module_author_archives" value="1">
<MTSetVar name="module_monthly_archives" value="1">

These are setting values for variables that will be used later. When you set a variable, you use the name attribute to specify the variable name, and the value attribute to give it a value. To retrieve a previously set value, we use <mt:getvar name="variable_name"> (or the shorter <mt:var> equivalent).

Looking at our template above, you won’t find any <mt:var> tags. You will, however, find a few includes:

<$MTInclude module="Header"$>

This is how we include a Template Module. The Header module contains the basic code we use at the top of all our pages. When you include a module, any variables set before the include are available within the module. So in our Header module, we find this line:

<body class="<$MTGetVar name="page_id"$> <$MTGetVar name="page_layout" default="layout-two-column-right"$>"<MTIf name="body_onload"> onload="<$MTGetVar name="body_onload"$>"</MTIf>>

Which sets class names and onload handlers for our <body> tag, based on previously set variables in the index template. There’s something else interesting in that line we should look at while we’re here:

<MTIf name="body_onload">
onload="<$MTGetVar name="body_onload"$>"
</mtif>

<MTIf> allows us to alter what we output based on the value of a tag or variable. In this case, we’re testing whether the variable body_onload has any value at all. If it does, we output the onload attribute and give it the value of the body_onload variable. We’ll look at MT control structures in more depth in a future post. Now, back to our index template:

<MTEntries>
    <$MTEntryTrackbackData$>
    <MTDateHeader>
        <h2 class="date-header"><$MTEntryDate format="%x"$></h2>
    </MTDateHeader>
    <$MTInclude module="Entry Summary"$>
</MTEntries>

This is where we loop through and output the most recent entries. By default, <MTEntries> displays the number of entries specified in your blog settings. You can alter what it displays with a variety of attributes:

  • lastn — Sets the number of entries to display.
  • days — Displays all entries from a certain number of days in the past. Be careful with this one — if you’re not someone who posts regularly, you can end up with a home page that has few, if any entries!
  • category — Specify which categories you want to pull entries from. You can use Boolean logic to get exactly the categories you want: <mt:entries category="(Tech OR Programming) NOT Web">

The full list of attributes is available in the Movable Type documentation.

Within the <MTEntries>, we output our Trackback data, then use the <MTDateHeader> container to display the entry date. We could just use <MTEntryDate> by itself, but <MTDateHeader> allows us to only display it once for each day of entries. So if you posted eight times on April 15 about how much you owed in taxes, the “April 15, 2008″ heading would only appear once, above the first post.

Then we do another include to pull in the Entry Summary template module, which displays each entry. After <MTEntries>, we display a link to our archives, using the following tag:

<$MTLink template="archive_index"$>

<MTLink> will output the URL for any template or entry generated by your blog, giving you an easy way to link to those pages. If you wanted to link to an entry you would use the entry_id attribute instead.

And, finally, we do one more include to get our footer. As you can see, there’s not a lot going on in the index template itself. Most of the nitty gritty occurs in the included modules. And while this does mean a lot of jumping around in MT to find the template you need to edit, you’ll only need to make that edit once for that change to appear on all your pages. If you do find yourself going back and forth between templates a lot, consider installing the Template Shelf plugin. It provides a sidebar that keeps frequently used templates just a click away.


  1. By Su posted on April 25, 2008 at 1:10 am
    Want an avatar? Get a gravatar! • You can link to this comment

    will output the URL for any template [...] generated by your blog

    This bears a little expansion.
    The Link tag’s template argument will accept either a template identifer, or template name, and will attempt them in that order. Your code sample is linking to an index template with identifier of “archive_template”, which in the defaults happens to have the name “Archive Index.”

    Linking to the identifier is generally going to be preferable, since it will remain constant if the template is renamed. The caveat is that custom index templates created through the application don’t currently allow you to specify an identifier, in which case your only option is to link via name.

  2. By BillyMabray posted on April 25, 2008 at 9:34 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Thanks, Su, that’s good to know. It’s interesting that the default Main Index template uses a template identifier, but only the template name is mentioned in the official documentation.

    And since template identifier corresponds to the index template type, I wonder what MTLink would link to if you created a second “archive_template” template? I’ll have to try that.

  3. By BillyMabray posted on April 25, 2008 at 9:50 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Ok, so I tried it. I created a second “rsd” template and rebuilt the main index. MTLink continued to link to the original rsd template. When I changed the original to a Custom Index Template, then it started linking to my new rsd template. My guess is when you use the template identifier, MTLink links to the first template of that type that it finds.

  4. TrackbackNETTUTS - Web development tutorials and links - Best of the Web - April