LIBlogs

Main | Working with Categories »

Making Your Sidebar an MTInclude

The sidebar code is extensive. If you want to place your sidebar on multiple archive templates or other files in your blog, this is a lot of code to manage in all those separate files. Also, perhaps you will want to add custom content to your sidebar at a later date. This means you would have to add this content to each individual template in which you have included your sidebar.

The solution to this challenge is something called an include. Includes allow an application to insert code from one file, called an include, into another using a single command. This is often used on Web servers to insert repeated code, such as a header or footer, into multiple pages.

Movable Type offers its own type of include function, inserted with the MTInclude tag. The MTInclude tag allows you to manage the repeated content in your blog in a modular way.

If you plan to place your sidebar into more than one template, it is recommended that you use the MTInclude feature to accomplish this. Here's how to do it in your Main Index template.

1. Locate your sidebar code.

Open the Templates page and view the Indexes window. From there, open your Main Index template.

Main Index Template

The sidebar code consists of all the code between the <div id="beta"> <div id="beta-inner" class="pkg"> tags and their respective closing div tags. The closing two div tags appear just after the this code: Powered by<br /><a href="http://www.sixapart.com/movabletype/">Movable Type <$MTVersion$></a>

<div id="beta">
<div id="beta-inner" class="pkg">

Your sidebar code, containing the search box, archives, recent posts, etc.

</div>
</div>

2. Copy the sidebar code.

<div id="beta">
<div id="beta-inner" class="pkg">

Your sidebar code

</div>
</div>

3. Create a template module for your sidebar code.

Go to the Templates Modules page and click on the "Modules" tab. Click on "Create New Template Module."

Template Modules

Create a new Template Module and paste in the sidebar code. Name your Module something obvious like "Sidebar".

Save the module.

4. Place the MTInclude code into your Main Index template.

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

<$MTInclude module="Sidebar"$>

This is what it looks like in context.

<div id="beta">
<div id="beta-inner" class="pkg">

<$MTInclude module="Sidebar"$>

</div>
</div>

Notice that the file name within quotations must exactly match the name of the new module you created, in this case, Sidebar.

5. Save and Rebuild your Main Index template.

6. Repeat these steps in any template in which you wish to include your sidebar.

Post a comment