Mustache.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * A Mustache implementation in PHP.
  4. *
  5. * {@link http://defunkt.github.com/mustache}
  6. *
  7. * Mustache is a framework-agnostic logic-less templating language. It enforces separation of view
  8. * logic from template files. In fact, it is not even possible to embed logic in the template.
  9. *
  10. * This is very, very rad.
  11. *
  12. * @author Justin Hileman {@link http://justinhileman.com}
  13. */
  14. class Mustache {
  15. protected $otag = '{{';
  16. protected $ctag = '}}';
  17. protected $tagRegEx;
  18. protected $template;
  19. protected $view;
  20. protected $partials;
  21. /**
  22. * Mustache class constructor.
  23. *
  24. * This method accepts a $template string and a $view object.
  25. *
  26. * @access public
  27. * @param mixed $template (default: null)
  28. * @param mixed $view (default: null)
  29. * @return void
  30. */
  31. public function __construct($template = null, $view = null, $partials = null) {
  32. $this->template = $template;
  33. $this->view = $view;
  34. $this->partials = $partials;
  35. }
  36. /**
  37. * Render the given template and view object.
  38. *
  39. * Defaults to the template and view passed to the class constructor unless a new one is provided.
  40. *
  41. * @access public
  42. * @param string $template (default: null)
  43. * @param mixed $view (default: null)
  44. * @return string Rendered Mustache template.
  45. */
  46. public function render($template = null, $view = null, $partials = null) {
  47. if ($template === null) $template = $this->template;
  48. if ($view === null) $view = $this->view;
  49. if ($partials === null) $partials = $this->partials;
  50. $template = $this->renderSection($template, $view);
  51. return $this->renderTags($template, $view);
  52. }
  53. /**
  54. * Render boolean and enumerable sections.
  55. *
  56. * @access protected
  57. * @param string $template
  58. * @param mixed $view
  59. * @return string
  60. */
  61. protected function renderSection($template, $view) {
  62. if (strpos($template, $this->otag . '#') === false) {
  63. return $template;
  64. }
  65. $otag = $this->prepareRegEx($this->otag);
  66. $ctag = $this->prepareRegEx($this->ctag);
  67. $regex = '/' . $otag . '\\#(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\1' . $ctag . '\\s*/m';
  68. $matches = array();
  69. while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
  70. $section = $matches[0][0];
  71. $offset = $matches[0][1];
  72. $tag_name = trim($matches[1][0]);
  73. $content = $matches[2][0];
  74. $replace = '';
  75. $val = $this->getVariable($tag_name, $view);
  76. if (is_array($val)) {
  77. foreach ($val as $local_view) {
  78. $replace .= $this->render($content, $local_view);
  79. }
  80. } else if ($val) {
  81. $replace .= $content;
  82. }
  83. $template = substr_replace($template, $replace, $offset, strlen($section));
  84. }
  85. return $template;
  86. }
  87. /**
  88. * Loop through and render individual Mustache tags.
  89. *
  90. * @access protected
  91. * @param string $template
  92. * @param mixed $view
  93. * @return void
  94. */
  95. protected function renderTags($template, $view) {
  96. if (strpos($template, $this->otag) === false) {
  97. return $template;
  98. }
  99. $otag = $this->prepareRegEx($this->otag);
  100. $ctag = $this->prepareRegEx($this->ctag);
  101. $this->tagRegEx = '/' . $otag . "(=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
  102. $html = '';
  103. $matches = array();
  104. while (preg_match($this->tagRegEx, $template, $matches, PREG_OFFSET_CAPTURE)) {
  105. $tag = $matches[0][0];
  106. $offset = $matches[0][1];
  107. $modifier = $matches[1][0];
  108. $tag_name = trim($matches[2][0]);
  109. $html .= substr($template, 0, $offset);
  110. $html .= $this->renderTag($modifier, $tag_name, $view);
  111. $template = substr($template, $offset + strlen($tag));
  112. }
  113. return $html . $template;
  114. }
  115. /**
  116. * Render the named tag, given the specified modifier.
  117. *
  118. * Accepted modifiers are `=` (change delimiter), `!` (comment), `>` (partial)
  119. * `{` or `&` (don't escape output), or none (render escaped output).
  120. *
  121. * @access protected
  122. * @param string $modifier
  123. * @param string $tag_name
  124. * @param mixed $view
  125. * @return string
  126. */
  127. protected function renderTag($modifier, $tag_name, $view) {
  128. switch ($modifier) {
  129. case '=':
  130. return $this->changeDelimiter($tag_name, $view);
  131. break;
  132. case '!':
  133. return $this->renderComment($tag_name, $view);
  134. break;
  135. case '>':
  136. return $this->renderPartial($tag_name, $view);
  137. break;
  138. case '{':
  139. case '&':
  140. return $this->renderUnescaped($tag_name, $view);
  141. break;
  142. case '':
  143. default:
  144. return $this->renderEscaped($tag_name, $view);
  145. break;
  146. }
  147. }
  148. /**
  149. * Escape and return the requested tag.
  150. *
  151. * @access protected
  152. * @param string $tag_name
  153. * @param mixed $view
  154. * @return string
  155. */
  156. protected function renderEscaped($tag_name, $view) {
  157. return htmlentities($this->getVariable($tag_name, $view));
  158. }
  159. /**
  160. * Render a comment (i.e. return an empty string).
  161. *
  162. * @access protected
  163. * @param string $tag_name
  164. * @param mixed $view
  165. * @return string
  166. */
  167. protected function renderComment($tag_name, $view) {
  168. return '';
  169. }
  170. /**
  171. * Return the requested tag unescaped.
  172. *
  173. * @access protected
  174. * @param string $tag_name
  175. * @param mixed $view
  176. * @return string
  177. */
  178. protected function renderUnescaped($tag_name, $view) {
  179. return $this->getVariable($tag_name, $view);
  180. }
  181. /**
  182. * Render the requested partial.
  183. *
  184. * @access protected
  185. * @param string $tag_name
  186. * @param mixed $view
  187. * @return string
  188. */
  189. protected function renderPartial($tag_name, $view) {
  190. $view = new self($this->getPartial($tag_name), $view);
  191. return $view->render();
  192. }
  193. /**
  194. * Change the Mustache tag delimiter. This method also replaces this object's current
  195. * tag RegEx with one using the new delimiters.
  196. *
  197. * @access protected
  198. * @param string $tag_name
  199. * @param mixed $view
  200. * @return string
  201. */
  202. protected function changeDelimiter($tag_name, $view) {
  203. $tags = explode(' ', $tag_name);
  204. $this->otag = $tags[0];
  205. $this->ctag = $tags[1];
  206. $otag = $this->prepareRegEx($this->otag);
  207. $ctag = $this->prepareRegEx($this->ctag);
  208. $this->tagRegEx = '/' . $otag . "(=|!|>|\\{|%)?([^\/#]+?)\\1?" . $ctag . "+/";
  209. return '';
  210. }
  211. /**
  212. * Get a variable from the view object.
  213. *
  214. * If the view is an array, returns the value with array key $tag_name.
  215. * If the view is an object, this will check for a public member variable
  216. * named $tag_name. If none is available, this method will execute and return
  217. * any class method named $tag_name. Failing all of the above, this method will
  218. * return an empty string.
  219. *
  220. * @access protected
  221. * @param string $tag_name
  222. * @param mixed $view
  223. * @return string
  224. */
  225. protected function getVariable($tag_name, $view) {
  226. if (is_object($view)) {
  227. if (isset($view->$tag_name)) {
  228. return $view->$tag_name;
  229. } else if (method_exists($view, $tag_name)) {
  230. return $view->$tag_name();
  231. }
  232. } else if (isset($view[$tag_name])) {
  233. return $view[$tag_name];
  234. }
  235. return '';
  236. }
  237. /**
  238. * Retrieve the partial corresponding to the requested tag name.
  239. *
  240. * Silently fails (i.e. returns '') when the requested partial is not found.
  241. *
  242. * @access protected
  243. * @param string $tag_name
  244. * @return string
  245. */
  246. protected function getPartial($tag_name) {
  247. if (is_array($this->partials) && isset($this->partials[$tag_name])) {
  248. return $this->partials[$tag_name];
  249. }
  250. return '';
  251. }
  252. /**
  253. * Prepare a string to be used in a regular expression.
  254. *
  255. * @access protected
  256. * @param string $str
  257. * @return string
  258. */
  259. protected function prepareRegEx($str) {
  260. $replace = array(
  261. '\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(',
  262. ')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?',
  263. '{' => '\{', '}' => '\}', ',' => '\,'
  264. );
  265. return strtr($str, $replace);
  266. }
  267. }