Engine.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace Symfony\Components\Templating;
  3. use Symfony\Components\Templating\Loader\LoaderInterface;
  4. use Symfony\Components\Templating\Renderer\PhpRenderer;
  5. use Symfony\Components\Templating\Renderer\RendererInterface;
  6. /*
  7. * This file is part of the symfony package.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. /**
  15. * Engine is the main class of the templating component.
  16. *
  17. * @package symfony
  18. * @subpackage templating
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class Engine
  22. {
  23. protected $loader;
  24. protected $renderers;
  25. protected $current;
  26. protected $helpers;
  27. protected $parents;
  28. protected $stack;
  29. protected $charset;
  30. /**
  31. * Constructor.
  32. *
  33. * @param LoaderInterface $loader A loader instance
  34. * @param array $renderers An array of renderer instances
  35. * @param array $helpers A array of helper instances
  36. */
  37. public function __construct(LoaderInterface $loader, array $renderers = array(), array $helpers = array())
  38. {
  39. $this->loader = $loader;
  40. $this->renderers = $renderers;
  41. $this->helpers = array();
  42. $this->parents = array();
  43. $this->stack = array();
  44. $this->charset = 'UTF-8';
  45. $this->cache = array();
  46. $this->addHelpers($helpers);
  47. if (!isset($this->renderers['php']))
  48. {
  49. $this->renderers['php'] = new PhpRenderer();
  50. }
  51. foreach ($this->renderers as $renderer)
  52. {
  53. $renderer->setEngine($this);
  54. }
  55. }
  56. /**
  57. * Renders a template.
  58. *
  59. * The template name is composed of segments separated by a colon (:).
  60. * By default, this engine knows how to parse templates with one or two segments:
  61. *
  62. * * index: The template logical name is index and the renderer is php
  63. * * index:twig: The template logical name is index and the renderer is twig
  64. *
  65. * @param string $name A template name
  66. * @param array $parameters An array of parameters to pass to the template
  67. *
  68. * @return string The evaluated template as a string
  69. *
  70. * @throws \InvalidArgumentException if the renderer does not exist or if the template does not exist
  71. * @throws \RuntimeException if the template cannot be rendered
  72. */
  73. public function render($name, array $parameters = array())
  74. {
  75. if (isset($this->cache[$name]))
  76. {
  77. list($name, $options, $template) = $this->cache[$name];
  78. }
  79. else
  80. {
  81. list($name, $options) = $this->splitTemplateName($old = $name);
  82. // load
  83. $template = $this->loader->load($name, $options);
  84. if (false === $template)
  85. {
  86. throw new \InvalidArgumentException(sprintf('The template "%s" does not exist (renderer: %s).', $name, $options['renderer']));
  87. }
  88. $this->cache[$old] = array($name, $options, $template);
  89. }
  90. $this->current = $name;
  91. $this->parents[$name] = null;
  92. // renderer
  93. $renderer = $template->getRenderer() ? $template->getRenderer() : $options['renderer'];
  94. if (!isset($this->renderers[$options['renderer']]))
  95. {
  96. throw new \InvalidArgumentException(sprintf('The renderer "%s" is not registered.', $renderer));
  97. }
  98. // render
  99. if (false === $content = $this->renderers[$renderer]->evaluate($template, $parameters))
  100. {
  101. throw new \RuntimeException(sprintf('The template "%s" cannot be rendered (renderer: %s).', $name, $renderer));
  102. }
  103. // decorator
  104. if ($this->parents[$name])
  105. {
  106. $slots = $this->get('slots');
  107. $this->stack[] = $slots->get('_content');
  108. $slots->set('_content', $content);
  109. $content = $this->render($this->parents[$name], $parameters);
  110. $slots->set('_content', array_pop($this->stack));
  111. }
  112. return $content;
  113. }
  114. /**
  115. * Outputs a rendered template.
  116. *
  117. * @param string $name A template name
  118. * @param array $parameters An array of parameters to pass to the template
  119. *
  120. * @see render()
  121. */
  122. public function output($name, array $parameters = array())
  123. {
  124. echo $this->render($name, $parameters);
  125. }
  126. /**
  127. * Gets a helper value.
  128. *
  129. * @param string $name The helper name
  130. *
  131. * @return mixed The helper value
  132. *
  133. * @throws \InvalidArgumentException if the helper is not defined
  134. */
  135. public function __get($name)
  136. {
  137. return $this->$name = $this->get($name);
  138. }
  139. public function addHelpers(array $helpers = array())
  140. {
  141. foreach ($helpers as $alias => $helper)
  142. {
  143. $this->set($helper, is_int($alias) ? null : $alias);
  144. }
  145. }
  146. /**
  147. * Sets a helper.
  148. *
  149. * @param HelperInterface $value The helper instance
  150. * @param string $alias An alias
  151. */
  152. public function set(HelperInterface $helper, $alias = null)
  153. {
  154. $this->helpers[$helper->getName()] = $helper;
  155. if (null !== $alias)
  156. {
  157. $this->helpers[$alias] = $helper;
  158. }
  159. $helper->setEngine($this);
  160. }
  161. /**
  162. * Returns true if the helper if defined.
  163. *
  164. * @param string $name The helper name
  165. *
  166. * @return Boolean true if the helper is defined, false otherwise
  167. */
  168. public function has($name)
  169. {
  170. return isset($this->helpers[$name]);
  171. }
  172. /**
  173. * Gets a helper value.
  174. *
  175. * @param string $name The helper name
  176. *
  177. * @return HelperInterface The helper instance
  178. *
  179. * @throws \InvalidArgumentException if the helper is not defined
  180. */
  181. public function get($name)
  182. {
  183. if (!isset($this->helpers[$name]))
  184. {
  185. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  186. }
  187. return $this->helpers[$name];
  188. }
  189. /**
  190. * Decorates the current template with another one.
  191. *
  192. * @param string $template The decorator logical name
  193. */
  194. public function extend($template)
  195. {
  196. $this->parents[$this->current] = $template;
  197. }
  198. /**
  199. * Escapes a string by using the current charset.
  200. *
  201. * @param string $value A string to escape
  202. *
  203. * @return string The escaped string or the original value if not a string
  204. */
  205. public function escape($value)
  206. {
  207. return is_string($value) || (is_object($value) && method_exists($value, '__toString')) ? htmlspecialchars($value, ENT_QUOTES, $this->charset) : $value;
  208. }
  209. /**
  210. * Sets the charset to use.
  211. *
  212. * @param string $charset The charset
  213. */
  214. public function setCharset($charset)
  215. {
  216. $this->charset = $charset;
  217. }
  218. /**
  219. * Gets the current charset.
  220. *
  221. * @return string The current charset
  222. */
  223. public function getCharset()
  224. {
  225. return $this->charset;
  226. }
  227. /**
  228. * Gets the loader associated with this engine.
  229. *
  230. * @return LoaderInterface A LoaderInterface instance
  231. */
  232. public function getLoader()
  233. {
  234. return $this->loader;
  235. }
  236. /**
  237. * Sets a template renderer.
  238. *
  239. * @param string $name The renderer name
  240. * @param RendererInterface $renderer A RendererInterface instance
  241. */
  242. public function setRenderer($name, RendererInterface $renderer)
  243. {
  244. $this->renderers[$name] = $renderer;
  245. $renderer->setEngine($this);
  246. }
  247. protected function splitTemplateName($name)
  248. {
  249. if (false !== $pos = strpos($name, ':'))
  250. {
  251. $renderer = substr($name, $pos + 1);
  252. $name = substr($name, 0, $pos);
  253. }
  254. else
  255. {
  256. $renderer = 'php';
  257. }
  258. return array($name, array('renderer' => $renderer));
  259. }
  260. }