Templates engine php

Suppose we are creating any website and in this website some elements are same like header/footer portions. These portions have to be almost same on all the pages throughout website, many peoples include separate developed pages and include these pages with php require attribute. This approach is good when all your headers/footers or the other pages you want to include have some static content but there are many often time when we have to change some portions of these pages to change not all. Example :
1) In website title we would like to same as our organization name appended with title of page as :

  • Ayunor Learning | Home
  • Ayunor Learning | Contact Us
  • Ayunor Learning | Our Offices

In above three points Ayunor Learning is same in all of the pages and the rest of title changes every time

2) Most of the Header file should remain same on page calling except their description as :

  • Ayunor Learning | This is the Home Page
  • Ayunor Learning | Please fill following form or mail us at abc@somesite.com to Reach Us
  • Ayunor Learning | Below are all the offices with their address where you can find us

We cannot solve this by using ordinary file inclusion, we need something different. Here are the way that templates engines are useful.

Template engines provide us way to customize only some region or all page with inheriting the other files layout/data with the help of tags. Consider the following example :

Step 1 : (creating files)

I have created 4 files as :
contactus.php (contact us page)
default.tpl (default template page)
index.php (home page)
template.php (mapping class, from basic page to default.tpl)

Step 2 : (creating default template file, default.tpl)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<style type="text/css">
.header {
	height: 50px;
	width: auto;
	background-color: aqua;
}

.footer {
	height: 50px;
	width: auto;
	background-color: #F4FFE6;
}
</style>
<title>Ayunor Learning [title] </title> 
</head>
<body>
	<div class="header">Ayunor Learning | [header]</div>  
	<h4>[page]</h4>
	<p>[content]</p>
	<div class="footer">Ayunor Learning | [footer]</div>
</body>
</html>

Step 3: (template.php, for mapping page request to templates)

<?php

class Template
{

    public $output; // for returning output after replacing tags
    public $file; // main tpl file
    public $values = array(); // array (values to be replaced after finding
                              // keys)
                              
    // when any object is made, get contents will get whole file
    function __construct ($file)
    {
        $this->file = $file;
        $this->output = file_get_contents($this->file);
    }
    
    // for setting values of keys with their corresponding
    // replaced values from main file like (index.php or contactus.php)
    function set ($key, $value)
    {
        $this->values[$key] = $value;
    }
    
    // every key, replacement is appended to $output and
    // $output will be send to calling file for actual file display
    function output ()
    {
        foreach ($this->values as $key => $values) {
            $tagsToReplace = "[$key]";
            $this->output = str_replace($tagsToReplace, $values, $this->output);
        }
        return $this->output;
    }
}

Step 4 : (index.php, home page)

<?php
require 'template.php';

$temp = new Template("default.tpl");
$temp->set('title', 'templates');
$temp->set('page', 'HOME/INDEX PAGE');

$actualContent = "This is the Home Page, click <a href=\"contactus.php\">here</a> to go to contact us page";

$temp->set('content', $actualContent);
$temp->set('header', 'this text is inside home header page');
$temp->set('footer', 'this text is inside home footer page');

echo $temp->output();

?>

Step 5 : (contactus.php, one more separate page)

<?php
require 'template.php';

$temp = new Template("default.tpl");
$temp->set('title', 'templates');
$temp->set('page', 'CONTACT US PAGE');

$actualContent = "Please fill following form or mail us at abc@somesite.com to Reach Us, 
        click <a href=\"index.php\">here</a> to go to index page";

$temp->set('content', $actualContent);
$temp->set('header', 'this text is inside header on contact us page');
$temp->set('footer', 'this text is inside footer on contact us page');

echo $temp->output();

?>

Output