<?php
require_once('template.class.php');
function addTemplateCommands(template &$template){
$template->add_command('assign', function ($params) use ($template){
$template->macros[$params[0]] = $params[1];
return '';
}, true);
$template->add_command('isset', function($params) use ($template){
return (bool)isset($template->macros[$params[0]]);
}, true);
$template->add_command('empty', function($params) use ($template){
return empty($template->macros[$params[0]]);
}, true);
$template->add_command('count', function($params) use ($template){
return count($template->macros[$params[0]]);
}, true);
$template->add_command('equate', function($params) use ($template){
$equation = implode('',$params);
return eval('return (float)('.$equation.');');
});
$compare = function ($params) use ($template){
if($params[1] == '==') return (bool)($params[0] == $params[2]);
if($params[1] == '!=') return (bool)($params[0] != $params[2]);
if($params[1] == '>') return (bool)($params[0] > $params[2]);
if($params[1] == '<') return (bool)($params[0] < $params[2]);
if($params[1] == '>=') return (bool)($params[0] >= $params[2]);
if($params[1] == '<=') return (bool)($params[0] <= $params[2]);
if($params[1] == '+') return (int)($params[0] + $params[2]);
if($params[1] == '-') return (int)($params[0] - $params[2]);
if($params[1] == '/') return (int)($params[0] / $params[2]);
if($params[1] == '*') return (int)($params[0] * $params[2]);
if($params[1] == '%') return (int)($params[0] % $params[2]);
if($params[1] == '.') return $params[0] . $params[2];
return (bool)@($params[3]);
};
$template->add_command('compare', $compare, true);
$template->add_command('', $compare, true);
$template->add_command('render', function($params) use ($template){
return $template->render($template->load($params[0]));
});
$inc = function ($params) use ($template){
return $template->macros[$params[0]]++;
};
$template->add_command('inc', $inc);
$template->add_command('++', $inc);
$template->add_command('print_r', function ($params) use ($template){
return '<pre>'.print_r($template->macros[$params[0]],true).'</pre>';
});
}
?>
|