PHP

Stuff.

Minimal Templates

The routine

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);
}

used with the code

$a['aaaa'] = 'AAAAA';
$a['bb'] = 'BB';
$a['ccccc'] = 'CCcCC';
print template('test.tpl', $a);

and the file test.tpl

Hallo %%aaaa%%, %%sdfsdfsdf%% sdfasdf %%bb%%, %%ccccc%%, %%ccccc%% %%dssad%% foo x%%x y%%aaaa%%y z%%%%z!

outputs

Hallo AAAAA,  sdfasdf BB, CCcCC, CCcCC  foo x%%x yAAAAAy z%%%%z!

Yay! :-)