Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement