« Display Code In Entries | Main | Category Customizations »

Using PHP and MT Includes

Updated March 1, 2007.

Using includes, you can modularize your template code making it easier to edit and rebuild your Movable Type templates. For example, instead of having all of your side bar and center content on the same Index template, you can set up a separate file to hold your side bar content and use an include to "include" it on your index template. This is especially handy if you use the same sidebar content on multiple index and archive templates.

There are two basic ways to use includes in Movable Type - the MTInclude tag and PHP includes. MTInclude is easy and anyone using MT can use it. The downside (slight) is that if you make a change to an include file, your sidebar for example, you have to rebuild every page on which that sidebar exists in order for the changes to appear. MTIncludes are also resource intensive. So if you have a large blog with thousands of entries, the use of MTIncludes will cause your rebuild times to be much longer than they would be otherwise. PHP, on the other hand, works dynamically. All you have to do is save the PHP include file and the changes automatically appear on your site. However, PHP includes usually require that the pages on which the include appears are php pages with php extensions. Therefore your web host or server must support PHP and not all do.

In this tutorial we'll use the example of setting up a sidebar as an include using both MTInclude and PHP methods.

1. MTInclude
Open your List Templates window. Open your Main Index template and select all of your sidebar code. (In the MT3.3 default Main Index template, this would be the code that comes after
<div id="beta">
<div id="beta-inner" class="pkg"> and before the closing tags associated with those tags.) Copy this code and paste it into a new template module. To do this, scroll down to the end of the List Templates window to find the Template Modules section. Create a new Template Module and paste in the sidebar code. Name your Module something obvious like "Sidebar Main". Save the module.


Go back to your Main Index template. Delete all of the sidebar code that you had previously copied and pasted into the module. In its place put the following code:

<$MTInclude module="Sidebar Main"$>

Save and rebuild your index template.

Note that you can also include a file rather than a module using MTInclude.

2. PHP Includes
Before you start make sure that your server or web host can handle PHP files. Then make sure that on whatever pages you will be using an include, those pages have a .php extension instead of .html. To do this on your index and archive pages, have the extension of the output files end with .php. (See HTML or PHP? for more explanation.)

As was explained in the MTInclude section, find the sidebar code section in your Main Index template. You will copy and paste that code into a separate file. You can choose to create either a new index template or a new template module in which to paste the sidebar code. If you choose a new index template you can choose whether or not to have that template be rebuilt every time you rebuild your site. For the index template create an output file with the extension .inc instead of .php or .html. For the template module, you will need to "Link to file" with the name and extension (.inc) of the file.

(On my sites I generally use php includes on index templates for the sidebars with automatic rebuilding selected and modules for elements like blogrolls or links that I then include into the sidebars using MTInclude.)

Open your Main Index template and delete all of the sidebar code that you had previously copied and pasted into the sidebar file or module. In its place put the following code:

<?php include "/path/to/your/file/name_of_your_sidebar_file.inc"; ?>

In the case of Learning Movable Type it looks like this:

<?php include "/home/elise/public_html/mt/sidebar.inc"; ?>

Sometimes the path names don't work and you need to use the URL instead in the include. And sometimes your include files will end with "php" and not "inc". For example:

<?php include "http://www.yoursite.com/file.php"; ?>

Note that using a URL in your PHP include instead of a path is less efficient in terms of server resources. Instead of simply pulling in a file from the disk of the server, the include has to create an HTTP session (just like a browser), get that file and then pull it in. Even if the file the include is pulling is on the same server, the result will be slower than using a pathname.

Save and rebuild your site.

There are other methods of using Server Side Includes (SSI) but they are more complicated. I have found that MTIncludes and PHP includes work well enough and using them has made it much easier for me to update and manage the sidebar and blogroll sections of my various sites.

Special thanks to Chad Everett of Everitz Consulting for his explanation of some of the finer points of includes.

Using PHP Code in Templates - from the MT Knowledge Base.
Converting to PHP tutorial

Comments (10)

Just wanted to say, you first put me on to the benefits of PHP includes and it has made such a difference to the flexibility of my site. I found it well worth learning this stuff!

Ever since I switched my sidebar to another page (almost a year back now I think) I've enjoyed it and tried to convert people :P and those that I have have loved it

Just a note about MTIncludes, if you have a lot of them - and a lot of big MTIncludes - rebuilt time drastically increases hence its usually best to go with SSI or PHP !

Excellent tutorial. Another advantage of PHP includes is that they do not add extra bytes to every page--for example, MTInclude-ing a section in an individual archive index would add it to every single static archive file that MT generates. A PHP include of the same file only requires one copy of that section of code to be on the server.

People who use a lot of MTIncludes might find the MTFastInclude plugin [http://www.nonplus.net/software/mt/MTFastInclude.htm ] speeds up their rebuilds somewhat.

I use serverside includes. They work exactly the same as PHP includes but don't require the page to be PHP thus reducing the workload of the server.

It has the added advantage of being included in any search site webcrawls because it's served as part of the HTML page.


Thanks for the very good explanation. I was trying for ages to understand the include and right navigation bar design.

One comment to the MTInclude setup:
- If you name the Template: Sidebar Main
you should also name the Outputfile "Sidebar Main" while using the tag:

The output file naming info I couldn't find on that page.

Thanks again and best wishes
Ulli Harz

elise [TypeKey Profile Page]:

Hi Ulli,

Actually, if you are using MTInclude to include a template module, all you have to do is give the Template Module a name. If you want to use a PHP include to include the module, you can link "Link this template to a file" and create a filename to use in your include.

This is an incredibly valuable technique, which has a dramatic effect on rebuild times: I run a climate change news aggregator with 3k entries, which are rebuilt every day: using mtinclude, where the navigation system is being rebuilt for every entry, rebuild time was 30 hours. Not good. Changing to php includes and using an index file to build the navigation module once per site rebuild has cut site rebuild time to 8 minutes. That's worth having ;



I tried following these instructions but they have seemed to change in version upgrades of moveable type. for the MTInclude I did a search for sidebar in my main index and didnt have this code. Can you update me on how to follow these instructions now?


Thank you very much for this article, it makes managing templates so much easier and better organised. At the moment i'm having one problem though, some MT variables in the sidebar (which uses php include) don't work, they just show up as the actual variable and don't show the information they are supposed to. For example, when it's supposed to show the latest entries all i get now is '' and categories do no show up either, any idea what went wrong?

sorry for posting again, but i fixed the problem myself, i had created the side bar as a 'module template', when i changed it to an 'index template' it worked fine.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)