As we've had an influx of new people looking into Movable Type 4.0 during the beta, either for the first time, or for the first time in years, one thing that keeps coming up is "the template tags look cool -- but what can I do with them?"
Things have been quiet around here as the team is super busy fixing bugs and getting closer to release candidates for MT 4.0, so it seemed like a good time to recap a little bit of the history of MT's template tags and then cover what's new and upcoming for template hackers.
First, the basics. MT's template tags were designed to be simple enough for anybody who knows HTML and CSS to be able to make a great looking blog or site, without knowing how to program. To enable that, all the basic template tags (which you can see on a site like MTTags.com) work like a simple mail merge program: You make your HTML (or XML, or whatever) page, and then substitute in MT's template tags so the program can insert your blog data automatically. This works for any entry, archive, or page in the system, and is pretty easy to understand.
Bonus: Because MT evaluates its template language internally, if you're doing a security audit when bringing in templates from a third party, MT's template tags can only do the things the application itself is allowed to do. That also means that MT's templates can generate code in any scripting language you want, whether that's PHP or ASP or JSP or whatever. And it lets us do cool things like checking the syntax on your template tags right within MT's editor.
But let's get to some simple examples, "Hello World"-style. To make an HTML page that says
<h1>Title of Your Entry</h1> <h2>March 4, 2009 09:25 AM</h2> <p>This is the text of the entry that you wanted to display on the page.</p>
All you need to do is put the appropriate tags in your MT template. Like so:
<h1><MTEntryTitle></h1>
<h2><MTEntryDate></h2>
<p><MTEntryBody></p>
Now, of course, you can get a lot more fancy than that. Oh, and here's something new: you can use XML-style namespace prefixes if you prefer. And tags are case-insensitive. So, if you're more used to XML namespaces, maybe your template looks like this:
<h1><mt:entrytitle></h1>
<h2><mt:entrydate></h2>
<p><mt:entrybody></p>
Sweet. But obviously, we want to do a lot more than just throwing our blog entries and titles up on a page. That's where some of the coolest improvements in MT4's templating comes in. You've got the full ability to get and set variables right in your templates. That's been around for a while, but it's easier than ever to use, and a heck of a lot more powerful.
<MTGetVar> and <MTSetVar> do exactly what you'd expect. (<MTGetVar> is also called <MTVar> if that's easier for you to remember.) You can name variables whatever you want, and if you've got a longer variable, use <MTSetVarBlock> to stash away a block of text, including HTML or MT template tags or whatever else you want. If you want that block of text to be evaluated for template tags on the fly (instead of just once, when you're setting the variable) for use in something like a loop, then just use <MTSetVarTemplate>. It's too much power for any one geek to handle!
There are tons of clever and complex things you can do with just these template tags (let alone our new and extended support for conditionals, loops, and boolean logic, which we'll get into more later) but here's some quick examples:
- Let's say you're publishing a CSS file for your site as a Movable Type template, and you want to set a variable to store a color value, so you can change it throughout the template later. Just put this at the top (using "#ccc;" to represent the grey color you're using):
<MTSetVar name="mycolor" value="#ccc">
And then throughout your template, you can use regular CSS with your variable, like so:
body, p { background-color : <MTGetVar name="mycolor">; } - You can even use variables inside of the attributes for MT's template tags. MT4 supports including entries from any blogs on your system in a template, aggregating your posts together using the IDs of your individual blogs. Including entries from blogs numbered 1, 2, and 4, would look like
<MTEntries blog_ids="1,2,4">. But you might want to change the blogs you're including in the future, so variables are handy. Just use
<MTSetVar name="blognumbers" value="1,2,4">
And then in your template, substitute the variable:
<MTEntries blog_ids="$blognumbers">
The template tag knows that the$means "this is a variable" and does the right thing, automagically.
Of course, there are infinite examples for these things, and MT4 lets you do If, Else, Unless loops and even Loops using variable arrays. But these first few examples should help get your gears turning as to what's truly possible using just the built-in template tags in MT4. Be sure to check out the Movable Type 4 Templating page and its corresponding wiki page for more info.


Sara
July 17, 2007 7:53 PM | Reply
Also makes it dead easy to switch between layout types. Example 3 column layout on the index page, 2 column layout for individual entry pages and maybe yet a different setup for "Pages" and archives.
adrian.sevitz.name
July 25, 2007 3:53 PM | Reply
I used server side includes to build my page up
so my index for example would be
-include globalheader
-include globalmain
- MT TEMPLATE STUFF
-include globalfooter.
Now with MT4 I can do that from the blog itself. Which would be good, if I had one blog. But my blog is made up of 4 different blogs. So I want to use the same template across all 4 blogs.
Ideally I want to make a blog called "global-templates" and set a whole lot of my templates up there. Then just call them from each individual blog.
I can see how to include a blog post from another blog
I can see how to include a template from the same blog
I can't see how to include a template from another blog.
I tried this but no avail
<MTInclude module="globalHeader" blog_ids="3">