I’ve been looking into using some PHP on this blog in order to reduce rebuild times. My sidebar, which is included on each page using Movable Type’s @MTIncludes@ tag is used to collate information from a variety of sources (although currently just my bloglines blogroll). Due to the way MT handles templates, this sidebar is being rebuilt for each and every page – so in the case of my blogroll, thats one request to bloglines per entry, category page, date archive etc. @MTIncludes@ can be used to include static files rather than other Movable Type modules, so my initial thought was to change my sidebar from a module template to an Index Template so it only gets rebuilt once. The problem with this is that @MTIncludes@ performs a static import – that is to say it imports the content when the page doing the including is built, but won’t pick up any changes to the included file afterwards. This is an even bigger problem when you consider how Movable Type rebuilds files – the index templates are the last to get done, so at best the individual entries will have an out of date include, and at worst might not find the file to be included at all.
What is needed is a dynamic include – a way to produce a page fragment once, and have pages automatically include this fragment when required. PHP seemed like the obvious answer, and the solution was surprisingly simple. As an experiment I created two new Index templates – a new experimental index, and my PHP sidebar which is the page fragment to be included. The new PHP index is setup to produce @index.php@, and I inserted the following @require@ statement to pull in my sidebar:
My PHP sidebar just contains the same code, but its output file is now called @sidebar.inc@. The result? Each request to @index.php@ will pull in the latest copy of my sidebar, meaning I can update @sidebar.inc@ without having to rebuild pages which reference it. Additionally, I can put more computationally intensive operations in the sidebar as it is only built once.
My next step is to roll this out across the rest of the site. Before I can do this I’m going to need to handle the fact that many of my current pages with a @html@ suffix are going to become @php@ files. Luckily I was planning to “de-cruft my URL’s(Cruft-free URLs in Movable Type)”:http://diveintomark.org/archives/2003/08/15/slugs anyway, and one of the steps for doing this involves setting up Apache redirects to hide the suffix from the outside world and making sure old references can still find the new files.