This site runs best with JavaScript enabled.

PHP Templating

Photo by Ben on Unsplash

Templating can be very simple with PHP, no other frameworks needed for basic templates.

Many of you may have heard of SMARTY. I've used it once. I just think it makes the hard things easy and the easy things hard. I suppose it is useful if you are in a multi-programmer environment, but I would rather just use run of the mill PHP rather than incorporating smarty templates.

Here is basically how I create every new website I do. I start by creating a basic layout - or I could even purchase a cheap one from where all templates are only $5. I also have a bunch on disk. I normally use these for ideas and create a new one from scratch. I often use FrontPage 2003 and Adobe Photoshop to help create the main template page, then I clean it up using TextPad- my favorite text editor.

After determining where all the main content will be I will break the page apart at that point into headers and footers.

Here is a basic example of what my header file will look like:


My footer could be simply as follows:


Then, to create a new page, I would use the following template for each new page:

2$ptitle = 'Page Title';
3require 'header.php';
5Content goes here...
7require 'footer.php';

That, in essence is simply all there is to it. You can make your header and footer file as elaborate as you need to. Some things you may notice here are:

  1. I set the $ptitle variable in my page file. It is inserted in the template between the title tags.
  2. I use the require function to include the header and footer files. You could also use include(), include_once(), or require_once(). See for the differences of these functions.

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter

Dustin Davis