June 04, 2004

Using PHP and MT Includes

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.1 default Main Index template, this would be the code that comes after
<div id="right">
<div class="sidebar"> 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. See instructions here in the MT User Manual.

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.
MT User Manual section on MTInclude
MT Support Forum thread on the use of PHP versus MTInclude
Converting to PHP tutorial

Has this tutorial been helpful? Please consider linking to Learning Movable Type at http://learningmovabletype.com/ . Thanks!

Posted by Elise Bauer on June 04, 2004 to General Tips and Tricks, PHP
Comments(7) | Email to a friend | Printer-friendly version


If you would like to send a trackback
please use the following URL: http://learningmovabletype.com/cgi-bin/mt32/mt-tb.cgi/250


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

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 has really helped me alot, it's exactly what i was looking for, it works, now i don't have to use those awkward frames!

Post a comment

(Before posting a comment please see the Comments and Trackbacks Policy. Do you need help troubleshooting your weblog? Please post questions and requests for support at the MT Support Forums. Thanks!)

Remember Me?

(you may use HTML tags for style)

Email to a friend

Email this article to:

Your email address:

Message (optional):