This is my intent to port the Radius template language to PHP.
Radius is a powerful tag-based template language inspired by the template languages used in MovableType and TextPattern. It uses tags similar to XML, but can be used to generate any form of plain text (HTML, e-mail, etc...).
For PHP 5.3 or higher.
require_once '/path/to/radius/lib/Radius/Autoloader.php';
Radius\Autoloader::register();
// Create a context and define some tags
$context = new Radius\Context();
$context->defineTags(array(
'hello' => function () {
return 'Hello world';
},
'repeat' => function ($tag) {
$number = $tag->getAttr('times', 1);
$result = '';
for ($i = 0; $i < $number; ++$i) {
$result .= $tag->expand();
}
return $result;
},
));
// Create a parser to parse tags that begin with 'r:'
$parser = new Radius\Parser($context, 'r');
// Parse and outputs the result
echo $parser->parse("A small example:\n<r:repeat times='3'>* <r:hello />!\n</r:repeat>");
Output:
A small example:
* Hello world!
* Hello world!
* Hello world!
This is not complete. If you feel interested and want to contribute, please contact me.