Component Writer's Guide :: Template Engines
RPCL provides a mechanism to use template engines, we provide, in the first release, a template plugin for Smarty, but more can be added easily.
Contents |
Choosing a template system to integrate
There are many to choose, here is a list extracted from a blog
Smarty
Smarty is a template engine that compiles the templates into PHP scripts, then executes those scripts. Very fast, very flexible.</p>
Heyes Template Class
A very easy to use, yet powerful and quick template engine that enables you to separate your page layout and design from your code.
FastTemplate
A simple variable interpolation template class that parses your templates for variables and spits out HTML with their values
ShellPage
A simple and easy to use class that lets you make whole websites based on template files for layouts. Change the template and your whole site changes.
STP Simple Template Parser
A simple, light weight and easy to use template parser class. It can assemble a page from several templates, output result pages to the browser or write them to the filesystem.
OO Template Class
An object oriented template class you can use in your own programs.
SimpleTemplate
A template engine to create and structure websites and applications. It can translate and compile the templates.
bTemplate
A small and fast template class that allows you to separate your PHP logic from your HTML presentation code.
Savant
A powerful but lightweight PEAR-compliant template system. It is non-compiling, and uses PHP itself as its template language.
ETS - easy template system
A template system that allows you to reshuffle templates with exactly the same data.
EasyTemplatePHP
A simple, yet powerful templating system for your site.
vlibTemplate
A fast, full featured template system that includes a caching and debugging class.
AvanTemplate
A template engine that is multi-byte safe and consumes little computing resource. It supports variable replacement and content blocks that can be set to hidden or shown.
Grafx Software’s Fast Template
A modification of the popular Fast Template system, this includes a cache function, debug console, and silent removal of unassigned dynamic blocks.
TemplatePower
A fast, simple and powerful template class. Features nested dynamic block support, block/file include support and show/hide unassigned variables.
TagTemplate
This library function was designed for use with template files and allows you to retrieve info from HTML files.
htmltmpl: templating engine
A templating engine for Python and PHP. Targeted to web application developers, who want to separate program code and design of their projects.
PHP Class for Parsing Dreamweaver templates
A simple class to parse a Dreamweaver template for use in custom mods for a Gallery 2 and a WordPress blog.
MiniTemplator (Template Engine)
A compact template engine for HTML files. It features a simple syntax for template variables and blocks. Blocks can be nested.
Layout Solution
Simplifies website development and maintenance. It holds commonly used variables and page elements so you don’t need to duplicate common layouts over and over.
Cached Fast Template
This inclusion into FastTemplate allows for caching of the template files, and can even cache with different specifications on separate blocks of content.
TinyButStrong
A template engine that supports MySQL, Odbc, Sql-Server and ADODB. It includes seven methods and two properties.
Brian Lozier’s php based template engine
Only 2k in size, very fast and object-orientated.
WACT
a template engine that separates code from design.
PHPTAL
a XML/XHTML template library for PHP.
Inheriting from PageTemplate
Template plugins must inherit from PageTemplate, here is the code:
/** * Base class for template engines, inherit from it and override initialize(), * assignComponents() and dumpTemplate() * */ class PageTemplate extends Component { protected $_filename=''; /** * Template filename * * @return string */ function readFileName() { return $this->_filename; } function writeFileName($value) { $this->_filename=$value; } /** * Called to initialize the template system * */ function initialize() {} /** * Called to assign component code to template holes * */ function assignComponents() {} /** * Called to dump the parsed Template to the output stream * */ function dumpTemplate() {} function __construct($aowner=null) { parent::__construct($aowner); } }
And, as you can see, there are few methods to override.
Initializing your Template Plugin
Override the initialize method to create your template engine or perform any initialization tasks you need:
function initialize() { require_once("smarty/libs/Smarty.class.php"); $this->_smarty = new Smarty; $this->_smarty->template_dir = ''; $this->_smarty->compile_dir = '/tmp'; }
Assigning components to the place holders
Assign components on the page to the place holders of your template:
function assignComponents() { $form=$this->owner; $this->_smarty->assign('HeaderCode', $form->dumpChildrenHeaderCode(true). $form->dumpHeaderJavascript(true)); $this->_smarty->assign('StartForm', $form->readStartForm()); $this->_smarty->assign('EndForm', $form->readEndForm()); reset($form->controls->items); while (list($k,$v)=each($form->controls->items)) { if ($v->Visible) { $this->_smarty->assign($v->Name, $v->show(true)); } } }
At least, three tags must be always inserted if you want to allow components to add javascript and events be processed. Those tags are HeaderCode, StartForm and EndForm. After that, iterate through all the controls inside the form and you can get the code for them using the show() method passing true as parameter, the control is not shown and the code is returned by the method.
Dumping out the results
Override dumpTemplate() to dump your processed template there:
function dumpTemplate() { $this->_smarty->display($this->FileName); }
PageTemplate has a FileName property that points to the file set in TemplateFilename for the form.
Registering your template plugin into the IDE
To allow the IDE show your template plugin in the drop-down, you must register it, this can be done using the method provided by the global object TemplateManager:
//Template registration global $TemplateManager; $TemplateManager->registerTemplate('SmartyTemplate','smartytemplate.inc.php');