Template.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Default base class for compiled templates.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. abstract class Twig_Template implements Twig_TemplateInterface
  18. {
  19. static protected $cache = array();
  20. protected $env;
  21. protected $blocks;
  22. /**
  23. * Constructor.
  24. *
  25. * @param Twig_Environment $env A Twig_Environment instance
  26. */
  27. public function __construct(Twig_Environment $env)
  28. {
  29. $this->env = $env;
  30. $this->blocks = array();
  31. }
  32. /**
  33. * Returns the template name.
  34. *
  35. * @return string The template name
  36. */
  37. public function getTemplateName()
  38. {
  39. return null;
  40. }
  41. /**
  42. * Returns the Twig environment.
  43. *
  44. * @return Twig_Environment The Twig environment
  45. */
  46. public function getEnvironment()
  47. {
  48. return $this->env;
  49. }
  50. /**
  51. * Returns the parent template.
  52. *
  53. * @return Twig_TemplateInterface|false The parent template or false if there is no parent
  54. */
  55. public function getParent(array $context)
  56. {
  57. return false;
  58. }
  59. /**
  60. * Displays a parent block.
  61. *
  62. * @param string $name The block name to display from the parent
  63. * @param array $context The context
  64. * @param array $blocks The current set of blocks
  65. */
  66. public function displayParentBlock($name, array $context, array $blocks = array())
  67. {
  68. if (false !== $parent = $this->getParent($context)) {
  69. $parent->displayBlock($name, $context, $blocks);
  70. } else {
  71. throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
  72. }
  73. }
  74. /**
  75. * Displays a block.
  76. *
  77. * @param string $name The block name to display
  78. * @param array $context The context
  79. * @param array $blocks The current set of blocks
  80. */
  81. public function displayBlock($name, array $context, array $blocks = array())
  82. {
  83. if (isset($blocks[$name])) {
  84. $b = $blocks;
  85. unset($b[$name]);
  86. call_user_func($blocks[$name], $context, $b);
  87. } elseif (isset($this->blocks[$name])) {
  88. call_user_func($this->blocks[$name], $context, $blocks);
  89. } elseif (false !== $parent = $this->getParent($context)) {
  90. $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
  91. }
  92. }
  93. /**
  94. * Renders a parent block.
  95. *
  96. * @param string $name The block name to render from the parent
  97. * @param array $context The context
  98. * @param array $blocks The current set of blocks
  99. *
  100. * @return string The rendered block
  101. */
  102. public function renderParentBlock($name, array $context, array $blocks = array())
  103. {
  104. ob_start();
  105. $this->displayParentBlock($name, $context, $blocks);
  106. return ob_get_clean();
  107. }
  108. /**
  109. * Renders a block.
  110. *
  111. * @param string $name The block name to render
  112. * @param array $context The context
  113. * @param array $blocks The current set of blocks
  114. *
  115. * @return string The rendered block
  116. */
  117. public function renderBlock($name, array $context, array $blocks = array())
  118. {
  119. ob_start();
  120. $this->displayBlock($name, $context, $blocks);
  121. return ob_get_clean();
  122. }
  123. /**
  124. * Returns whether a block exists or not.
  125. *
  126. * @param string $name The block name
  127. *
  128. * @return Boolean true if the block exists, false otherwise
  129. */
  130. public function hasBlock($name)
  131. {
  132. return isset($this->blocks[$name]);
  133. }
  134. /**
  135. * Returns all block names.
  136. *
  137. * @return array An array of block names
  138. */
  139. public function getBlockNames()
  140. {
  141. return array_keys($this->blocks);
  142. }
  143. /**
  144. * Returns all blocks.
  145. *
  146. * @return array An array of blocks
  147. */
  148. public function getBlocks()
  149. {
  150. return $this->blocks;
  151. }
  152. /**
  153. * Displays the template with the given context.
  154. *
  155. * @param array $context An array of parameters to pass to the template
  156. * @param array $blocks An array of blocks to pass to the template
  157. */
  158. public function display(array $context, array $blocks = array())
  159. {
  160. try {
  161. $this->doDisplay($context, $blocks);
  162. } catch (Twig_Error $e) {
  163. throw $e;
  164. } catch (Exception $e) {
  165. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
  166. }
  167. }
  168. /**
  169. * Renders the template with the given context and returns it as string.
  170. *
  171. * @param array $context An array of parameters to pass to the template
  172. *
  173. * @return string The rendered template
  174. */
  175. public function render(array $context)
  176. {
  177. $level = ob_get_level();
  178. ob_start();
  179. try {
  180. $this->display($context);
  181. } catch (Exception $e) {
  182. while (ob_get_level() > $level) {
  183. ob_end_clean();
  184. }
  185. throw $e;
  186. }
  187. return ob_get_clean();
  188. }
  189. /**
  190. * Auto-generated method to display the template with the given context.
  191. *
  192. * @param array $context An array of parameters to pass to the template
  193. * @param array $blocks An array of blocks to pass to the template
  194. */
  195. abstract protected function doDisplay(array $context, array $blocks = array());
  196. /**
  197. * Returns a variable from the context.
  198. *
  199. * @param array $context The context
  200. * @param string $item The variable to return from the context
  201. *
  202. * @return The content of the context variable
  203. *
  204. * @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
  205. */
  206. protected function getContext($context, $item)
  207. {
  208. if (!array_key_exists($item, $context)) {
  209. if (!$this->env->isStrictVariables()) {
  210. return null;
  211. }
  212. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
  213. }
  214. return $context[$item];
  215. }
  216. /**
  217. * Returns the attribute value for a given array/object.
  218. *
  219. * @param mixed $object The object or array from where to get the item
  220. * @param mixed $item The item to get from the array or object
  221. * @param array $arguments An array of arguments to pass if the item is an object method
  222. * @param string $type The type of attribute (@see Twig_TemplateInterface)
  223. * @param Boolean $isDefinedTest Whether this is only a defined check
  224. */
  225. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
  226. {
  227. // array
  228. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  229. if ((is_array($object) && array_key_exists($item, $object))
  230. || ($object instanceof ArrayAccess && isset($object[$item]))
  231. ) {
  232. if ($isDefinedTest) {
  233. return true;
  234. }
  235. return $object[$item];
  236. }
  237. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  238. if ($isDefinedTest) {
  239. return false;
  240. }
  241. if (!$this->env->isStrictVariables()) {
  242. return null;
  243. }
  244. if (is_object($object)) {
  245. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  246. // array
  247. } else {
  248. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  249. }
  250. }
  251. }
  252. if (!is_object($object)) {
  253. if ($isDefinedTest) {
  254. return false;
  255. }
  256. if (!$this->env->isStrictVariables()) {
  257. return null;
  258. }
  259. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object));
  260. }
  261. // get some information about the object
  262. $class = get_class($object);
  263. if (!isset(self::$cache[$class])) {
  264. $r = new ReflectionClass($class);
  265. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  266. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  267. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  268. }
  269. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  270. self::$cache[$class]['properties'][$property->getName()] = true;
  271. }
  272. }
  273. // object property
  274. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  275. if (isset(self::$cache[$class]['properties'][$item])
  276. || isset($object->$item) || array_key_exists($item, $object)
  277. ) {
  278. if ($isDefinedTest) {
  279. return true;
  280. }
  281. if ($this->env->hasExtension('sandbox')) {
  282. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  283. }
  284. return $object->$item;
  285. }
  286. }
  287. // object method
  288. $lcItem = strtolower($item);
  289. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  290. $method = $item;
  291. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  292. $method = 'get'.$item;
  293. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  294. $method = 'is'.$item;
  295. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  296. $method = $item;
  297. } else {
  298. if ($isDefinedTest) {
  299. return false;
  300. }
  301. if (!$this->env->isStrictVariables()) {
  302. return null;
  303. }
  304. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  305. }
  306. if ($isDefinedTest) {
  307. return true;
  308. }
  309. if ($this->env->hasExtension('sandbox')) {
  310. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  311. }
  312. $ret = call_user_func_array(array($object, $method), $arguments);
  313. if ($object instanceof Twig_TemplateInterface) {
  314. return new Twig_Markup($ret);
  315. }
  316. return $ret;
  317. }
  318. }