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.