Feature
Post

Category
Code


Optimal Includes for Movable Type

As far back as I can remember, Movable Type has always had the ability to include one template (or a file) in another template. You do it like this:

<mt:include module="footer">

But, there are problems with this method. First, it can slow down publishing, as the MT engine traverses all the included templates in each page it publishes. And if you’re publishing static files but including something that should be kept up-to-date (like your Action Stream), then you can end up with pages that look stale.

What I like to do is output the fragment I want to include to a text file, then pull that text file into whatever page on my site I want. But different pages publish differently, so it’s sometimes tricky to include the same file on all those pages. I handle this with a little snippet of MT template code. Let’s start by taking a look at that code:

<MTIf name="is_php">
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/actionstream.inc'); ?>
<MTElse>
    <MTIf name="system_template">
        <MTInclude file="/actionstream.inc">
    <MTElse>
        <!--#include virtual="/actionstream.inc" -->
    </MTIf>
</MTIf>

Our first line checks a is_php variable. We have to set that ourselves on any page we output as PHP — a contact form, for example. So let’s assume we have a Contact template that publishes to contact.php. At the top of that template we would add this:

<mt:setvar name="is_php" value="1">

When this variable is set, our code will use a standard PHP include to pull in the file:

<?php include($_SERVER['DOCUMENT_ROOT'] . '/actionstream.inc'); ?>

If it’s not a PHP page, we then check the system_template variable. This is already set in all the default system templates, so we don’t have to set it ourselves. System templates are served via CGI — they’re not published to static files, they’re displayed dynamically. Search results and comment previews use system templates. In this case, the standard MT include is the best option:

<MTInclude file="/actionstream.inc">

Notice we’re using the file attribute rather than the module attribute. This tells MT to look for a file on your web server.

Finally, if neither of the first two conditions apply, we use a Server Side Include (SSI):

<!--#include virtual="/actionstream.inc" -->

You will have to configure your server for SSI, but if you were able to install MT, you should be able to set up SSI.

Now that we know how to do our includes, let’s create something we can reuse whenever we need to do a file include. Create a new Template Module, name it Include, and put in the following modified version of our code above:

<MTIf name="is_php">
    <?php include($_SERVER['DOCUMENT_ROOT'] . '<mt:var name="includefile">'); ?>
<MTElse>
    <MTIf name="system_template">
        <MTInclude file="$includefile">
    <MTElse>
        <!--#include virtual="<mt:var name="includefile">" -->
    </MTIf>
</MTIf>

Now, anywhere we need to do a file include, we just put two lines of code:

<mt:setvar name="includefile" value="/actionstream.inc">
<mt:include module="Include">

The <mt:include> has access to any variables set before it’s called, so we can set includefile to the file we want to include, then call our Include module.

You’ve probably seen the <mt:var> tags before, but this might be new to you:

<MTInclude file="$includefile">

Here, we’re using an MT variable as the value for a tag attribute. This can be done on any MT tag, just append a dollar sign to the beginning of the variable name.

Again, this is really useful for things that don’t need to be part of the MT publishing process. In particular, any time you’re using an external API (Twitter, Flickr, etc.) to put content into your sidebar. In those cases, it’s more efficient to set up an external process to write that content to a file, then pull it in with our optimal include. As your site grows, you’ll be grateful for any time you can save on the publishing process.


  1. By Toni Hambilton posted on July 25, 2008 at 4:46 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Thank you so much for these MT tutorials Billy, they’re a great read and I get to actually learn something in the process.

  2. TrackbackMT 4.2 Brings New Features, Some Gotchas | Devlounge