Created
March 23, 2017 08:05
-
-
Save oshiro-kazuma/326542dcf5933848adeefb099efd9c93 to your computer and use it in GitHub Desktop.
SimpleTemplateEngine
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
<?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