PHP Classes

ExTemplate: Extensible template engine with callable functions

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 241 This week: 1All time: 8,017 This week: 560Up
Version License PHP version Categories
extemplate 1.0Public Domain5.4PHP 5, Templates
Description 

Author

This class implements an extensible template engine with callable functions.

It can register new template commands assigned by a name and a callable function that will define the meaning and result of the command when it is found on a template.

The template engine parses a template file to find marks enclosed in curly braces and replaces them with the result of the registered commands defined by the template marks.

Innovation Award
PHP Programming Innovation award nominee
April 2015
Number 3


Prize: One book of choice by Packt
Many template engines can be extended by the means of plugins that implement special commands for template tags.

This package implements a template engine that can be extended dynamically with new commands implemented with custom registered callable functions.

Manuel Lemos
Picture of Matthew Knowlton
  Performance   Level  
Name: Matthew Knowlton is available for providing paid consulting. Contact Matthew Knowlton .
Classes: 6 packages by
Country: United States United States
Age: 28
All time rank: 2036291 in United States United States
Week rank: 411 Up48 in United States United States Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php

require_once('template.class.php');
require_once(
'template.commands.php');

session_start();

$template = new template('template.demo.php');


addTemplateCommands($template);

$template->add_command('includeStyles', function ($params) use ($template){
    return
'<link rel="stylesheet" type="text/css" href="'.$params[0].'"/>'."\r\n";
});

$template->add_command('includeScript', function ($params) use ($template){
    return
'<script type="text/javascript" src="'.$params[0].'"></script>';
});


$template->set_macros(array(
   
'HeaderFile' => 'demo.header.php',
   
'FooterFile' => 'demo.footer.php',
   
'ScriptsFile' => 'demo.scripts.js',
   
'StylesFile' => 'demo.styles.css',
   
'PageTitle' => 'Benefits of using a Templating Engine',
   
'BenefitsOfTemplates' => array(
       
'Abstraction between Code & Output',
       
'Simplification of Output Handling',
       
'Sandboxing User Created Layouts',
       
'Easier for Non-programmmers to Write/Read',
       
'Easy data handoff between Front-End & Back-End Developers',
       
'Simple data escaping implementation'
   
)
));


$template->add_macro('isLoggedIn', (bool)(isset($_SESSION['UserName'])));
$template->add_macro('signInLink', 'demo.signin.php');

if(isset(
$_SESSION['UserName'])){
   
$template->add_macro('UserName', $_SESSION['UserName']);
}

echo
$template->render();

exit();

?>


  Files folder image Files  
File Role Description
Plain text file template.class.php Class Class Source
Accessible without login HTML file template.docs.html Doc. Documentation
Accessible without login Plain text file template.commands.php Aux. A few useful commands
Accessible without login Plain text file template.demo.php Example Example template file
Accessible without login Plain text file template.example.php Example Example PHP for proccessing a template
Accessible without login Plain text file demo.footer.php Aux. Footer file for the demo
Accessible without login Plain text file demo.header.php Aux. Template header file for the demo

 Version Control Unique User Downloads Download Rankings  
 0%
Total:241
This week:1
All time:8,017
This week:560Up