嗷嗷答 - 嗷嗷好的公务员考试网络答题平台

mustache模板语言(1)

作者:不烦恼 发布时间:September 7, 2011 分类:快乐学习

mustache -- Logic-less templates.

Mustache,是一个模板语言,可以应用在Ruby, JavaScript, Python, Erlang, PHP等多种编程语言中。
最近在kohana主页的源代码中接触到这个神奇的语言,深深被其简洁(无逻辑)的风格所吸引,于是学习了一下mustache(5)语法,并打算在未来应用到实际中。
PHP中应用mustache模板需要在程序中调用mustache.php,以此为例记录一下mustache模板的用法:

  1. <?php
  2. /*
  3.  * @param string $template (default: null) 模板内容
  4.  * @param mixed $view (default: null) 模板数据
  5.  * @param array $partials (default: null) 子模板
  6.  * @param array $options (default: array()) 选项)
  7.  */
  8. // 用法一:
  9. $template = '<html><head><title>{{title}}</title></head><body>{{> body}}</body></html>';
  10. $view = new StdClass();
  11. $view->title = '标题';
  12. $view->content = '内容';
  13. $partials = array(
  14. 'body' => '正文{{content}}',
  15. );
  16. $m = new Mustache();
  17. $m->render($template, $view, $partials);
  18.  
  19. //用法二:
  20. $template = '<html><head><title><?title?></title></head><body><?> body?></body></html>';
  21. $view = array();
  22. $view['title'] = '标题';
  23. $view['content'] = '内容';
  24. $partials = array(
  25. 'body' => '正文<?content?>',
  26. );
  27. $options = array(
  28. 'charset' => 'utf-8',
  29. 'delimiters' => '<? ?>',
  30. );
  31. $m = new Mustache($template, $view, array $partials, array $options);
  32. $m->render();

TODO:mustache模板语言的“语法”mustache模板语言(2)

添加新评论 »