Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SimpleTemplateEngine
<?php
/**
* Class SimpleTemplateEngine
*
* 変数展開ができるだけのテンプレートエンジン
*
* ex)
* SimpleTemplateEngine::apply('こんにちは{{ name }}さん', ['name' => '大城']);
* >> こんにちは大城さん。
*
*/
class SimpleTemplateEngine
{
static private function create_replace_strings($key) {
return [
sprintf('{{%s}}', $key),
sprintf('{{ %s }}', $key),
sprintf('{{ %s}}', $key),
sprintf('{{%s }}', $key),
];
}
static public function apply($template, $variables)
{
foreach ($variables as $key => $variable) {
$template = str_replace(self::create_replace_strings($key), $variable, $template);
}
return $template;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment