PHP Template Engine

There are so many PHP Frameworks and PHP Template Engine available to build simple to complex business applications. But, Building a web application in core PHP is the best thing that every developer loves to do.

This article does not cover PHP Frameworks. I am just sharing here my version of the PHP Template engine.

Using my engine, I will show you two things

  1. Separating PHP code from design (HTML).
  2. Single entry and exit of an application, just like C, C++ main function.

Lets build an application of rendering pages Home Page, Page 1 and Page 2 using my approach

Example using PHP Template Engine

Two types of files required

PHP File

Here I am going to use a single file “index.php” to handle all the operations of handling form, request, response, and database handling.

Template Files

These are files used for presentations.

They contain custom tags. These tags are replaced by an array.

We will do this by rendering these files.

PHP File (index.php)

All the operations are handle by index file only. Now, I defined the constant using the define function, core functions, classes, and Templates.

Define Function

You can create the different configuration files and set the required constant in that file, but I will create and set all required constant in index.php like DOMAIN_URL, and APP_NAME.

define('DOMAIN_URL','http://localhost/web/');
define('APP_NAME', "Crud Example using PHP Template Engine");

Core Functions

Here i am using two core functions in this example

  • “ResponseRedirect” function redirect the request.
  • “Template” function replaces all the placeholders tag with an array.
function ResponseRedirect($url) {
 header('Location: ' . $url);
}

function Template($template, $variables) {
 $template = SYSTEMPATH.$template;
 $content = file_get_contents($template);
 if ( is_array($variables) ) {
  foreach($variables as $key => $value) {
   $content = preg_replace('/{'.$key.'}/',$value,$content);
  }
 }
 return $content;
}

Classes

To display and render the page, I will use AppClass class. This class is wholesome responsible for handling all the functionality like handling requests and rendering.

class AppClass {
 function __construct(){
   $this->main();
 }
 function __destruct(){
 }
 function main() {
   $cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : "";
   switch($cmd) {
     case 'page1' : $this->page1(); break;
     case 'page2' : $this->page2(); break;
     default : $this->home(); break;
   }
 }
 function page1(){
  $vars['TITLE'] = "Page 1";
  $vars['DATA'] = "This is page 1";
  $vars['HOMEPAGEURL'] = DOMAIN_URL;
  $vars['HOMEPAGE'] = $vars['TITLE'];
  $output = Template('page1.html',$vars);
  echo $output;
 }
 function page2(){
  $vars['TITLE'] = "Page 2";
  $vars['DATA'] = "This is page 2";
  $vars['HOMEPAGEURL'] = DOMAIN_URL;
  $vars['HOMEPAGE'] = $vars['TITLE'];
  $output = Template('page2.html',$vars);
  echo $output;
 }
 function home(){
  $vars['TITLE'] = "Home Page";
  $vars['DATA'] = "This is Home Page";
  $vars['PAGE1URL'] = DOMAIN_URL."?cmd=page1";
  $vars['PAGE1'] = "Page 1";
  $vars['PAGE2URL'] = DOMAIN_URL."?cmd=page2";
  $vars['PAGE2'] = "Page 2";
  $output = Template('homepage.html',$vars);
  echo $output;
 } 
}

Templates

Here i am using three HTML pages

hompage.html

<!-- template for home page -->
<html>
<head><title>{TITLE}</title></head>
<body>
<h1>{DATA}</h1>
<a href='{PAGE1URL}'>{PAGE1}</a> &nbsp; &nbsp; <a href='{PAGE2URL}'>{PAGE2}</a>
</body>
</html>

page1.html

<!-- template for page1 -->
<html>
<head><title>{TITLE}</title></head>
<body>
<h1>{DATA}</h1>
<a href='{HOMEPAGEURL}'>{HOMEPAGE}</a> 
</body>
</html>

page2.html

<!-- template for page2 -->
<html>
<head><title>{TITLE}</title></head>
<body>
<h1>{DATA}</h1>
<a href='{HOMEPAGEURL}'>{HOMEPAGE}</a> 
</body>
</html>

How it works

Now, let see how the code works

First, save all the PHP code in index.php file and save the file on your web root directory including your template files.

In order to call your main function you need to create object of AppClass.

$object = new AppClass();

As you can see that in the constructor of AppClass, main function will be called.

In the main function, based on the parameter passed in the query string the member function will get executed.

The first time, the home function renders the template with the help of the Template function and replace the required tag with an array. Here tags are {TITLE}, {HOMEPAGEURL} {HOMEPAGE} given in HTML File.

PHP Template Engine Example