Mustache.php 7.7 KB

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