Compatible with: PHP 4, PHP 5
nanotemp — A simple template engine
Source code of nanotemp()
<?php
function nanotemp($template, $data, $file = true)
{
if(!isset($data['marker'])) $data['marker'] = '$$';
if($file) $input = explode($data['marker'], file_get_contents($template));
else $input = explode($data['marker'], $template);
for($i = 0, $ret = '', $num = count($input); $i < $num; $i++)
{
if($i % 2 === 0) $ret.= $input[$i];
else if(isset($data[$input[$i]])) $ret.= $data[$input[$i]];
}
return $ret;
}
?>
Example #1 nanotemp() example
<?php
$template = 'Marker: $$marker$$<br />
Template engine: $$engine$$<br />
Coded by: $$coded$$';
$output["engine"] = "NanoTemp";
$output["coded"] = "Sh1fty";
echo nanotemp($template, $output, false);
?>
The above example will output:
Marker: $$<br /> Template engine: NanoTemp<br /> Coded by: Sh1fty<br />