Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class SimpleTemplateEngine
  5. *
  6. * 変数展開ができるだけのテンプレートエンジン
  7. *
  8. * ex)
  9. * SimpleTemplateEngine::apply('こんにちは{{ name }}さん', ['name' => '大城']);
  10. * >> こんにちは大城さん。
  11. *
  12. */
  13. class SimpleTemplateEngine
  14. {
  15. static private function create_replace_strings($key) {
  16. return [
  17. sprintf('{{%s}}', $key),
  18. sprintf('{{ %s }}', $key),
  19. sprintf('{{ %s}}', $key),
  20. sprintf('{{%s }}', $key),
  21. ];
  22. }
  23. static public function apply($template, $variables)
  24. {
  25. foreach ($variables as $key => $variable) {
  26. $template = str_replace(self::create_replace_strings($key), $variable, $template);
  27. }
  28. return $template;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement