PHP¶
Stuff.
Minimal Templates¶
function template($templatefile, $variables)
{
$tpl = file_get_contents($templatefile, FILE_TEXT);
$keys = preg_replace('/^(.*)$/', '%%1%%', array_keys($variables));
$vals = array_values($variables);
$repl = str_replace($keys, $vals, $tpl);
return preg_replace('/%%[^% ]+?%%/', '--', $repl);
}
Usage:
File test.tpl:
Hallo %%aaaa%%, %%sdfsdfsdf%% sdfasdf %%bb%%, %%ccccc%%, %%ccccc%% %%dssad%% foo x%%x y%%aaaa%%y z%%%%z!
Code:
$a['aaaa'] = 'AAAAA';
$a['bb'] = 'BB';
$a['ccccc'] = 'CCcCC';
print template('test.tpl', $a);
Produce:
Hallo AAAAA, sdfasdf BB, CCcCC, CCcCC foo x%%x yAAAAAy z%%%%z!
created: 2008-03-07, updated: 2015-10-10