Mustache.php 7.7 KB

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