Templights is template system that uses PHP as template language and has a powerful multiple inheritance support. Small memory footprint and efficient implementation results in very good performance of template system.
SyntaxTemplates consists of blocks which can be nested or extended. Blocks are
... [More]
defined by open tag {% and closed by close tag %}. After open tag name of block must be defined. Block name must be uppercase alphanumeric word. Content of block is defined inside curly braces.
Example:
{% BLOCK {
This is content of a block named BLOCK.
{% SUB {
This is sub-block named SUB.
}%}
}%}Blocks can be extended simply by replacing block name with parent template filename.
Example:
{% template.tpl {
{% BLOCK {
This content will override content of block named BLOCK from template file template.tpl
}%}
}%}Inside blocks PHP tags can be included. It is recommended to use PHP alternative syntax for control structures for more readable code.
Example:
{% LIST {
}%}Comments are enclosed by {%% and %%} tags.
Example:
{%% This is one line comment! %%}
{%%
Comments
can be spreaded
over many lines.
%%}APIBasetpl_path (path) : Getter/setter function for templights base path tpl_load (file) : Wrapper function for loading template form file tpl_render (template, context) : Render template within specified context tpl_read (file) : Reads a template file - templates can be read only from path defined with tpl_path function tpl_token (code) : Creates list of tokens from template code tpl_parse (tokens, nodelist) : Parses tokens and creates list of string nodes. Blocks are parsed recursively tpl_tostring (template) : Convert list of nodes into string Extratpl_compile (code) : Wrapper function for creating template form template code tpl_extend (template, template2) : Extend template from another template tpl_extend_block (template, block_name, block) : This function is used for overriding content of specific template block tpl_dump (template, file) : Wrapper function for dumping template into a file Examples:
require 'templights/base.php';
$tpl = tpl_load('template.tpl');
echo tpl_render($tpl, array('title' => 'Templights example'));With extra API templates can be constructed and manipulated inside PHP.
require 'templights/extra.php';
$code = <<
{% HEAD {}%}
EOF;
$code2 = <<
}%}
EOF;
$tpl1 = tpl_compile($code);
$tpl2 = tpl_compile($code2);
$tpl = tpl_extend($tpl1, $tpl2);
echo tpl_render($tpl, array('title', 'Example of templights extra API');ConstantsTPL_BASETPL_BASE constant is deprecated since 0.1.3 release, use tpl_path function instead
All templates must be located inside one directory. This directory can be defined with TPL_BASE constant. If template in not located on path defined, Exception is thrown.
Example:
// constant TPL_BASE must be defined before including templights file
define('TPL_BASE', dirname(__FILE__).'/templates/');
require 'templights/base.php';DevelopementOne of new features currently available in repository is variable block names. Blocks with variable names are evaluated within specified context. This gives us new possibilities and greater flexibility.
Syntax:
{% @VAR {
...
}%}After evaluation:
// @VAR -> BLOCK
{% BLOCK {
...
}%}
// @VAR -> header.tpl
{% header.tpl {
..
}%}Some examples:
{% header_en.tpl {}%}
{% header.tpl {}%}
// better way
{% @header {}%}
{% BODY {
...
}%}
// if @var -> BODY then block BODY will be replaced
{% @var {
...
}%}
This feature introduces new function tpl_eval which evaluates template within context. Evaluation of template has to be done manually - tpl_load does not evaluate template.
$tpl = tpl_load('template-name.tpl');
$tpl = tpl_eval($tpl, array('var' => 'BLOCK'));
echo tpl_render($tpl, $context);
Developement version has also support for accessing nested blocks.
{% BLOCK {
{% SUB { ... }%}
}%}
// or
{% BLOCK.SUB { ... }%}
Names for nested blocks can also be defined using variables.
// @var -> BLOCK.SUB
{% @var { ... }%}
// @var -> BLOCK
{% @var.SUB { ... }%}
// @var -> include.tpl - overrides block BLOCK.SUB in parent template include.tpl
{% @var.BLOCK.SUB { ... ... }%}
To try out new features checkout with command: svn checkout http://templights.googlecode.com/svn/branch-0.2/ templights-read-only
Changelog0.2-svnnew extra api functions tpl_eval and tpl_extend_code support for variable block names support for accessing nested blocks support for reading / dumping compiled templates using TPL_BASE constant triggers fatal error strict parsing and syntax error reporting new tag introduced for switching on/off the parser 0.1.3.1Fixed issue 1 Unit tests added 0.1.3TPL_BASE constant is deprecated new api function tpl_path renamed library files support for empty blocks: {% EMPTYBLOCK %} correct whitespace rendering examples updated bugfix: if template was extended, blocks on same level were overridden bugfix: invalid regex for tokenizing comment tags 0.1.2rewrite of parser, improved performance for ~100% added comment tag Bugfix: content of blocks was not overridden in multilevel inheritance 0.1.1Bugfix: tpl_render function was not returning evaluated code, the code was printed. RequirementsPHP 5 (with PCRE) TODOvariable block names (@VAR) remove TPL_BASE constant compiled templates variable blocks default values strict parser support for custom syntax engines for Smarty, Django syntax template security Documentation [Less]