Developing a Simple PHP Template Engine

Simple PHP Template Engine

A PHP Template Engine is a utility used to separate the presentation layer from an application logic. In the business environment the use of template engines benefits in two main ways:

Easier maintenance. Have you ever worked with the mix of PHP and HTML code? If yes, you will understand the pain of million developers. Or in other words, the template engine allows you to separate the HTML from PHP, so you can easily maintain your application without any headache.

Delegating responsibilities. At the same time the web developer can work on the application logic and the web designer can work on the presentation layer.

The picture shares the basic overview of any template engine.

PHP Template Engine Overview

HTML Templates

Our PHP Engine Template will have one “base” template called main.tpl and one additional template called header.tpl. However, you can use any file extension, but the most common is a .tpl.

<!-- Skip the head, html, etc. -->
        <span>User Name: {username}</span>
        <span>Age: {age}</span>
        <span>City: {city}</span>

As you can see, the tag format used in this tutorial is {TAG_NAME}, and you can always change it.

PHP Template Engine Class

Our template engine will consist of one template class with two private properties:

class template
    // Tags array
    private $tags = [];

    // Template file
    private $template;

The public function getFile() takes a string containing the file direction as a parameter. And if the file exists, it will return the file content. Otherwise it will return false value.

// Get the template file
public function getFile($file)
		$file = file_get_contents($file);
		return $file;
		return false;

The class constructor is a wrap for the getFile() function. There is nothing interesting, except one thing: you can specify what to do if the template is not available.

public function __construct($templateFile)
	$this->template = $this->getFile($templateFile);

	// If the template file is not accessible
	if(!$this->template) {
		return "Error! Can't load the template file $templateFile";


The public set() function is used to set up the tags and values.

// Set the {tag} with value
public function set($tag, $value)
	$this->tags[$tag] = $value;

The private replaceTags() function¬†is the core of our template engine because it makes all “magic” of replacing tags with values.

// Replaces all {tags} with corresponding values from $tags array
private function replaceTags()
	foreach ($this->tags as $tag => $value) {
		$this->template = str_replace('{'.$tag.'}', $value, $this->template);

	return true;

The final public render() function is used to display the template. This function calls replaceTags() function right before printing the template.

// Render the build template
public function render()

	echo $this->template;

The use of a PHP Template Engine Class

Firstly, we have to initialize the template object, and then we can use it.

require_once 'template.php';
$tpl = new template('main.tpl');

Secondly, we can set up variables and files as a tags. The following code snippet just shows the main idea how to use the tags; however, in real application you will fill the tags dynamically with information from a database or any other information source.

$tpl->set('username', 'Alexander');
$tpl->set('age', '10');
$tpl->set('city', 'Toronto');
// Set {header} as a header.tpl file
$tpl->set('header', $tpl->getFile('header.tpl'));

Finally, we need to render the template.


PHP Template Engine: Conclusion

In this tutorial we’ve created a really Simple PHP Template Engine which can be a great start point for your own powerful template engine.

Have any questions? Feel free to ask them in the comments!

Download the sources


  1. Alex says:

    Great PHP lesson, I really like it!

  2. Alex says:


    One question: is it possible to set all variables using an array? E.g.:

    ‘username’ => ‘Alexander’,
    ‘age’ => ’10’,
    ‘city’ => ‘Toronto’


    1. v_simeiko says:

      No, but you always can extend functionality to allow setting an array of template variables.

  3. Norman says:

    That’s nice. I lack somehow a possibility to use templates with a loop.

  4. Dieter says:

    Try foreach (array as $key => $value){
    $tpl->set($key, $value);

Leave a Reply