The TemplateEngine2 is a PHP based Templating system that is highly customizable, if it doesn't do what you need it to - then add it!
Have a look at the plugins/
directory to see how that works ;)
- clone this repository somewhere
- call
sudo make install-phpunit
(or make sure phpunit is installed) - call
make
- watch the build process ;)
- take the
TemplateEngine2.php
andTE_setup.php
files from thebuild/
directory and include them in your project - done start using the TemplateEngine2 =)
// assuming you put those 2 files into the include directory in your project:
require_once('include/TemplateEngine2.php');
// create your TemplateEngine2 instance
$te= new TemplateEngine();
$te->setRootPath('./'); // the root path is the root of your project browser relative tho the current script
$te->setTemplatePath('templates/standard'); // the directory where the template resides (relative to the ROOT_PATH ^)
// set any data that the template will be able to use by calling $te->set($name, $value)
// $name may only use uppercase letters 0-9 and the _ (that is with the included plugins...)
$tasks= array();
$tasks[]= array('NAME' => 'Task1', 'DESCRIPTION' => 'description1');
$tasks[]= array('NAME' => 'Task2', 'DESCRIPTION' => 'description2');
$te->set('MY_TASKS', $tasks); // $tasks may actually be anything (any type), the template decides how it will be presented
$te->output('yourTemplate.tpl'); // will process and output ./templates/standard/yourTemplate.tpl
// a tip:
// in case you wan't to see which variables are available to a template append the "te_dump" parameter to the URL
// like: http://localhost/project/index.php?te_dump or http://localhost/project/index.php?arg1=111&te_dump
// this will also show you the few built in variables ;)
The files for this example:
The example template ./templates/standard/yourTemplate.tpl
:
{LOAD=header.tpl}
There are {MY_TASKS|LEN} Tasks.
<ul>
{FOREACH[MY_TASKS]=task.tpl}
</ul>
{LOAD=footer.tpl}
What happens in this template?
{LOAD=header.tpl}
: loads the fileheader.tpl
from your templates directory into the output.{MY_TASKS|LEN}
: this takes theMY_TASKS
variable and processes it with theLEN
escape method, the result is the number of entries in the array.{FOREACH[MY_TASKS]=task.tpl}
: theFOREACH
directive takes theMY_TASKS
, validates it as an array and addstask.tpl
once for every element.{LOAD=footer.tpl}
: loads the filefooter.tpl
from your templates directory into the output.
The ./templates/standard/header.tpl
:
<!doctype html>
<html>
<head>
<!-- ... -->
</head>
<body>
The ./templates/standard/task.tpl
:
<li>{NAME}: {DESCRIPTION}</li>
The FOREACH
directive makes all entries of the array element (and a few extra things) available as template variables, other variables can be accessed as well.
The ./templates/standard/footer.tpl
:
</body>
</html>
For more read the included documentation (German) or read the code ;) that will not only show you what is available but also how exactly they work and it will show you how easy it is to extend the built in functionality.
Issues, Comments, Pull Requests all welcome^^