Learning Movable Type: 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.

template_modules.gif

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.


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


Posted by Elise Bauer on June 4, 2004 12:11 AM to Learning Movable Type http://www.learningmovabletype.com/