Simple Templating Engine using PHP

3 Min Read

Here’s a simple, light-weight, innovative and fast templating engine for PHP, client-side JavaScript and Node.js. It is fully working and extensible, and like other templating engines out there, it is multipurpose and elegant. This Repository does a minimum amount of caching and parsing to build dynamic templates that can be used in both PHP and JavaScript.

The complete source code and necessary project files of the proposed Templating Engine can be downloaded from the link below. A project report is not available for this one, so you can refer the description provided here and use it to create a synopsis of the project.

Download Simple Templating Engine using PHP with Source Code

[sociallocker]

Download Simple Templating Engine using PHP with Source Code and Screenshots

[/sociallocker]

PHP Templating Engine – Project Abstract:

This templating engine is very versatile, but you may encounter a few performance issues sometimes as it is built with minimum features. So, PHP, itself being a templating language, was used for the development. The result is a very fast, simple, and multipurpose templating engine.

The repository does minimum parsing and caching to develop dynamic templates and tries to contain the much needed functionality inside the common language subset. It can cache templates both in PHP and JavaScript dynamically. File system caching has 3 modes:

  1. NONE: uses only in-memory caching,
  2. NOUPDATE: caches the templates only once, and
  3. AUTOUPDATE: re-creates the cached template if original template has changed.

Features:

  • simple
  • light-weight
  • innovative
  • multipurpose
  • elegant
  • easily extensible and configurable

Classes:

This templating engine uses just 2 classes — one for PHP and one for JavaScript: Contemplate.php and Contemplate.js respectively. No other dependencies!

Sample Source Code – Test.php:

<?php

/*
*  Simple light-weight templating engine
*/

define('ABSPATH', dirname(__FILE__));

// include the Contemplate Class
include ABSPATH.'/src/Contemplate.php';

$sepleft="<%";
$sepright="%>";
// set the template separators
Contemplate::setTemplateSeparators($sepleft, $sepright);
// set the cache directory (make sure to exist)
Contemplate::setCacheDir(ABSPATH.'/_tplcache');
// dynamically update the cached template if original template has changed
Contemplate::setCacheMode(Contemplate::CACHE_TO_DISK_AUTOUPDATE);
// add the templates paths
Contemplate::add(array(
    'main'=>ABSPATH.'/_tpls/main.tpl.html',
    'demo'=>ABSPATH.'/_tpls/demo.tpl.html',
    'sub'=>ABSPATH.'/_tpls/sub.tpl.html',
));

// the data to be used by the templates
$data=array(
    'users'=>array(
        array( 
            array('name'=>'u1', 'text'=>'text1', 'id'=>'id1'),
            array('name'=>'u2', 'text'=>'text2', 'id'=>'id2'),
            array('name'=>'u3', 'text'=>'text3', 'id'=>'id3'),
        ),
        array( 
            array('name'=>'u4', 'text'=>'text4', 'id'=>'id4'),
            array('name'=>'u5', 'text'=>'text5', 'id'=>'id5'),
            array('name'=>'u6', 'text'=>'text6', 'id'=>'id6'),
        ),
        array( 
            array('name'=>'u7', 'text'=>'text7', 'id'=>'id7'),
            array('name'=>'u8', 'text'=>'text8', 'id'=>'id8'),
            array('name'=>'u9', 'text'=>'text9', 'id'=>'id9'),
        ),
    )
);

$main_template_data=array(
    'templates'=>array(
        'demo'=>Contemplate::getTemplateContents('demo'),
        'sub'=>Contemplate::getTemplateContents('sub'),
    ),
    'sepleft'=>$sepleft,
    'sepright'=>$sepright,
    'data_js'=>json_encode($data),
    'render_php'=>Contemplate::tpl('demo', $data)
);

echo Contemplate::tpl('main', $main_template_data);

exit;

Similar Projects:
Online Shopping Cart
Statistics Reporter
Daily Horroscope

You can use test.php to test the basic functionality of the template. This file is available in the download link or you can copy-paste the source code presented above.

More PHP projects and mini projects can be found here.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version