Skip to content

Instantly share code, notes, and snippets.

@oshiro-kazuma
Created March 23, 2017 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshiro-kazuma/326542dcf5933848adeefb099efd9c93 to your computer and use it in GitHub Desktop.
Save oshiro-kazuma/326542dcf5933848adeefb099efd9c93 to your computer and use it in GitHub Desktop.
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