Open Power Template
From Wikipedia, the free encyclopedia
The introduction to this article provides insufficient context for those unfamiliar with the subject. Please help improve the article with a good introductory style. |
Open Power Template is a template engine written in PHP 5. It is used to generate dynamic web application content, like HTML or RSS, by placing the script results inside special tags, such as {$data}. The syntax includes not only programming-language-like instructions, but also high-level structures: sections, components. The syntax may work in XML-syntax mode, where the tag delimiters emulate XML.
The engine is easy to extend and very fast. The templates are firstly compiled into a PHP code stored on the hard drive for the further usage. The compilation algorithm is inspired by XML parsers, so it allows to process the template like a tree of nodes, which improves the flexibility.
[edit] Sample application
Here is a sample application generating the output using Open Power Template with XML-syntax mode enabled:
<table width="60%"> <tr> <td width="30"><b>#</b></td> <td width="20%"><b>Name</b></td> <td width="*"><b>Description</b></td> </tr> <opt:section name="products"> <tr> <td width="30">{$products.id}</td> <td width="20%">{$products.name}</td> <td width="*">{$products.description}</td> </tr> </opt:section> </table>
PHP code:
<?php define('OPT_DIR', '../lib/'); require('../lib/opt.class.php'); try { $tpl = new optClass; $tpl -> root = './templates/'; $tpl -> compile = './templates_c/'; $tpl -> gzipCompression = 1; // Enable XML-style delimiters $tpl -> xmlsyntaxMode = 1; // Enable strict XML syntax for the instructions $tpl -> strictSyntax = 1; $tpl -> httpHeaders(OPT_HTML); require('db_connect.php'); $r = mysql_query('SELECT id, name, description FROM products ORDER BY id'); $list = array(); while($row = mysql_fetch_row($r)) { // add the next item $list[] = array( 'id' => $row[0], 'name' => $row[1], 'description' => $row[2] ); } $tpl -> assign('products', $list); if(rand(0, 10) < 5) { $tpl -> assign('border' , 'border="1"'); } $tpl -> parse('example11.tpl'); mysql_close(); } catch(optException $exception) { optErrorHandler($exception); } ?>
[edit] Project status
The first version stable version, 1.0.0, was released on 2 August 2006, after a year and a half of working and testing. The newest version is 1.1.4, released on 30 March 2008. This is the latest of the 1.x series. The next version will be 2.0.
The library has a complete English documentation.