PHP / Apache Templating

Here’s a quick tip to have a simple template-based website. I use this method on my Textpattern-powered site and it works great!

This method does not requires any installation of PHP template engines such as Smarty, XTemplate, etc. All it takes is 2 lines in your .htaccess file.

Well, how am I doing it?

Sort answer: using Apache directives.

Longer answer: using Apache directives to auto-append and auto-prepend header and footer files.

To achieve this, do the following:

  1. Create an HTML/PHP file with your design layout.
  2. Break this file into 3 portions, before dynamic content, content, and after dynamic content.
  3. Save the before portion as header.php and the after portion as footer.php in your server’s root directory.
  4. In your .htaccess, add the following lines:
    php_value auto_prepend_file /header.php
    php_value auto_append_file /footer.php
  5. That’s it.

Now, create your website as you normally would. Every time your pages will load, Apache will automatically “wrap” them with your header and footer files, producing your desired end result.

I recommend using the .html file extension for the header and footer files to save server load if your pages are not dynamic pages.

PLEASE NOTE: Your webhost may not allow you to use these Apache directives, so be sure to check that out before hand.

Pages: 1
Author: Ran Aroussi
Ran Aroussi is a computer developer & designer working with computers and the Internet since 1996. Ran interests include (but not limited to) web development, standards and programming.