Created
February 28, 2017 20:12
-
-
Save marcoskubis/114b0ed98a5d751f3650710c05095359 to your computer and use it in GitHub Desktop.
Simple template engine for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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