Tips on optimizing "whole site publishing" and creating a shared header and footer.

When making design changes to your web site, there comes a time when you might have to click the dreaded Republish All button. It is "dreaded" of course because for large sites this process could take a very long time. A customer recently contacted us even saying that it took 6-7 hours to republishing their site, which was frustrating to them to say the least.

Of course, there need not be any dread associated with this process, not only because there are ways to optimize the publishing speed of your site, but there are some relatively simple techniques one can employ to eliminate the need to republish all together.

Shared Headers and Footers

The most often aspect of a site that needs to get edited is the header or footer. Publisher need to add a new nav item, or change their ad banner, or add a link to the footer or something like that. Changing something as fundamental as the header or footer often requires the entire site to be republished because you want that to be the same across every page of your site. Luckily there is a way to manage these aspects of your site such that changing them does not require you to republish everything.

What you need

Before we get started, this system requires that you have PHP installed. I will assume for now that you know how to do this, or how to ask your web host to get it installed.

Second, you need your site to publish .php files, or you need to instruct your web server to parse .html as if they were .php files. That is done by adding this to your .htaccess or your httpd.conf:

AddHandler php5-script .html

Once your files are being properly parsed as PHP files, then you can use PHP includes, which is what this technique requires.

What you will do

For those interested in understanding the ins-and-outs of this technique, here is a little technical background on what we are about to do.

There are two types of pages that Movable Type publishes: static files and dynamic pages. Static files result in a physical file being published to your web server. These files will be parsed as PHP and will end up including another file which contains the HTML for your header and another file for your footer. When these included files are updated, you automagically affect every page on your site that is including them. That means to add a link you just edit the footer.php file and then presto, your footer is updated site wide.

The next type of page Movable Type publishes is a dynamic one. These are typically powered by mt-search.cgi or mt-comments.cgi, etc. They are rendered directly by Movable Type using perl. Because they are rendered by Perl, that means that they cannot contain any PHP directives, because a limit of almost all web servers is that a page can only be processed by a single module/processor/scripting language, either CGI or PHP or ASP or JSP.

The technique we are about to show you will make sure that no matter what is rendering the page, your header and footer files will be properly shared and displayed to your visitors as you intend.

Setting up your templates

First, we should setup some global templates. I choose to use global templates because often I have multiple blogs in Movable Type that need to share the same header and footer.

  1. Create a Global Template Module called "Banner Include." In this module place the HTML you would like to have shared across all the pages of your web site. Be sure this code contains no PHP. Where content of the page needs to be dynamic, consider using jQuery or another client side method of rendering the header's content.

  2. Create a Global Module called "Banner." This is the template module that all other templates will include. It contains the smarts to know whether or not to return a php directive to include a header.php file, or to return a Movable Type directive that will embed the same HTML into the template directly (which is needed only for system templates). In this template place the following code:

     <mt:setvarblock name="GlobalSitePath"><mt:Multiblog include_blogs="$ROOTBLOG"><$mt:BlogSitePath$></mt:Multiblog></mt:setvarblock>
     <mt:unless name="system_template">
     <mt:setvarblock name="nav_include"><$mt:var name="GlobalSitePath"$>header.php</mt:setvarblock>
     <?php include('<$mt:var name="nav_include"$>'); ?>
     <$mt:include module="Banner Include"$>

    What is important to note here is that you need to replace the text $ROOTBLOG with the blog ID of the blog responsible for publishing your shared PHP include file. See the next step.

  3. Navigate to the template listing for the blog you are creating your shared header for and create an Index Template called "Shared Header." Have it output a file called "header.php". In this template include the following template code:

     <$mt:include module="Banner Include"$>
  4. Finally, let's edit your templates in the blog or blogs that will use the new shared header. Open up each Index and Archive template. Place within in them the following code where you want your header to appear:

     <$mt:include module="Banner"$>

    Now, open up each system template and place within them the following code where you want your header to appear:

     <$mt:include module="Banner" system_template="1"$>

Wow. Ok. This seems like a lot of magic and mildly recursive. So what is going on here? How does it work.

  1. You maintain a single template module (a global one) called "Banner Include." This template is what you edit when you want to modify the contents of your site's header.

  2. You publish a header.php file to your web site. This template simply includes the Banner Include file and results in a file being published to your system that contains the HTML for your header.

  3. Finally, you have another global template module called "Banner" which is responsible for either outputting a php include directive for header.php or for slurping up the contents of your header.php file and dropping it directly into your template. You do this because your system templates, processed via Perl, cannot include any PHP code, including a php include directive. So in this case, you need to use the native <mt:include file> which is the Movable Type counterpart to a php include directive.

Hopefully this all makes sense. We find this technique, used for both headers and footers, is a life saver and dramatically reduces the number of time we need to republish an entire web site. We hope you find it helpful as well.

If you have any problems using the technique above, please do not hesitate to ask us. As always, we are here to help any Movable Type or Melody user that needs it.

Pro Tip: Making Whole Site Republishing Easier

At Endevver we created a plugin called Send2PQ, which is short for Send to Publish Queue. It is a simple plugin that allows one to quickly dispatch their entire blog to be republished via a background task. When the site has been completely rebuilt, you will be notified via email. The plugin requires that you have run-periodic-tasks running. We think you will find that this system of republishing is much easier, more reliable and even faster; and to top it all off, it does not tie up a browser window while it is running.