Skip to content

Instantly share code, notes, and snippets.

@marcoskubis
Created February 28, 2017 20:12
Show Gist options
  • Save marcoskubis/114b0ed98a5d751f3650710c05095359 to your computer and use it in GitHub Desktop.
Save marcoskubis/114b0ed98a5d751f3650710c05095359 to your computer and use it in GitHub Desktop.
Simple template engine for PHP
function template($template, $array = [])
{
$res = preg_match_all("/{{(.*?)}}/", $template, $m);
foreach ($m[1] as $i => $varname) {
$val = isset($array[trim($varname)])? $array[trim($varname)] : '';
$template = str_replace($m[0][$i], sprintf('%s', $val), $template);
}
return $template;
}
template("<p>Hello, {{name}}.</p>", ["name" => "Marcos"]); // Hello, Marcos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment