Template.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 $parents;
  21. protected $env;
  22. protected $blocks;
  23. protected $traits;
  24. /**
  25. * Constructor.
  26. *
  27. * @param Twig_Environment $env A Twig_Environment instance
  28. */
  29. public function __construct(Twig_Environment $env)
  30. {
  31. $this->env = $env;
  32. $this->blocks = array();
  33. $this->traits = array();
  34. }
  35. /**
  36. * Returns the template name.
  37. *
  38. * @return string The template name
  39. */
  40. abstract public function getTemplateName();
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getEnvironment()
  45. {
  46. return $this->env;
  47. }
  48. /**
  49. * Returns the parent template.
  50. *
  51. * This method is for internal use only and should never be called
  52. * directly.
  53. *
  54. * @return Twig_TemplateInterface|false The parent template or false if there is no parent
  55. */
  56. public function getParent(array $context)
  57. {
  58. $parent = $this->doGetParent($context);
  59. if (false === $parent) {
  60. return false;
  61. } elseif ($parent instanceof Twig_Template) {
  62. $name = $parent->getTemplateName();
  63. $this->parents[$name] = $parent;
  64. $parent = $name;
  65. } elseif (!isset($this->parents[$parent])) {
  66. $this->parents[$parent] = $this->env->loadTemplate($parent);
  67. }
  68. return $this->parents[$parent];
  69. }
  70. abstract protected function doGetParent(array $context);
  71. /**
  72. * Displays a parent block.
  73. *
  74. * This method is for internal use only and should never be called
  75. * directly.
  76. *
  77. * @param string $name The block name to display from the parent
  78. * @param array $context The context
  79. * @param array $blocks The current set of blocks
  80. */
  81. public function displayParentBlock($name, array $context, array $blocks = array())
  82. {
  83. if (isset($this->traits[$name])) {
  84. $this->traits[$name][0]->displayBlock($name, $context, $blocks);
  85. } elseif (false !== $parent = $this->getParent($context)) {
  86. $parent->displayBlock($name, $context, $blocks);
  87. } else {
  88. throw new Twig_Error_Runtime(sprintf('The template has no parent and no traits defining the "%s" block', $name), -1, $this->getTemplateName());
  89. }
  90. }
  91. /**
  92. * Displays a block.
  93. *
  94. * This method is for internal use only and should never be called
  95. * directly.
  96. *
  97. * @param string $name The block name to display
  98. * @param array $context The context
  99. * @param array $blocks The current set of blocks
  100. */
  101. public function displayBlock($name, array $context, array $blocks = array())
  102. {
  103. if (isset($blocks[$name])) {
  104. $b = $blocks;
  105. unset($b[$name]);
  106. call_user_func($blocks[$name], $context, $b);
  107. } elseif (isset($this->blocks[$name])) {
  108. call_user_func($this->blocks[$name], $context, $blocks);
  109. } elseif (false !== $parent = $this->getParent($context)) {
  110. $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
  111. }
  112. }
  113. /**
  114. * Renders a parent block.
  115. *
  116. * This method is for internal use only and should never be called
  117. * directly.
  118. *
  119. * @param string $name The block name to render from the parent
  120. * @param array $context The context
  121. * @param array $blocks The current set of blocks
  122. *
  123. * @return string The rendered block
  124. */
  125. public function renderParentBlock($name, array $context, array $blocks = array())
  126. {
  127. ob_start();
  128. $this->displayParentBlock($name, $context, $blocks);
  129. return ob_get_clean();
  130. }
  131. /**
  132. * Renders a block.
  133. *
  134. * This method is for internal use only and should never be called
  135. * directly.
  136. *
  137. * @param string $name The block name to render
  138. * @param array $context The context
  139. * @param array $blocks The current set of blocks
  140. *
  141. * @return string The rendered block
  142. */
  143. public function renderBlock($name, array $context, array $blocks = array())
  144. {
  145. ob_start();
  146. $this->displayBlock($name, $context, $blocks);
  147. return ob_get_clean();
  148. }
  149. /**
  150. * Returns whether a block exists or not.
  151. *
  152. * This method is for internal use only and should never be called
  153. * directly.
  154. *
  155. * This method does only return blocks defined in the current template
  156. * or defined in "used" traits.
  157. *
  158. * It does not return blocks from parent templates as the parent
  159. * template name can be dynamic, which is only known based on the
  160. * current context.
  161. *
  162. * @param string $name The block name
  163. *
  164. * @return Boolean true if the block exists, false otherwise
  165. */
  166. public function hasBlock($name)
  167. {
  168. return isset($this->blocks[$name]);
  169. }
  170. /**
  171. * Returns all block names.
  172. *
  173. * This method is for internal use only and should never be called
  174. * directly.
  175. *
  176. * @return array An array of block names
  177. *
  178. * @see hasBlock
  179. */
  180. public function getBlockNames()
  181. {
  182. return array_keys($this->blocks);
  183. }
  184. /**
  185. * Returns all blocks.
  186. *
  187. * This method is for internal use only and should never be called
  188. * directly.
  189. *
  190. * @return array An array of blocks
  191. *
  192. * @see hasBlock
  193. */
  194. public function getBlocks()
  195. {
  196. return $this->blocks;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function display(array $context, array $blocks = array())
  202. {
  203. // we don't use array_merge as the context being generally
  204. // bigger than globals, this code is faster.
  205. foreach ($this->env->getGlobals() as $key => $value) {
  206. if (!array_key_exists($key, $context)) {
  207. $context[$key] = $value;
  208. }
  209. }
  210. try {
  211. $this->doDisplay($context, $blocks);
  212. } catch (Twig_Error $e) {
  213. throw $e;
  214. } catch (Exception $e) {
  215. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
  216. }
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function render(array $context)
  222. {
  223. $level = ob_get_level();
  224. ob_start();
  225. try {
  226. $this->display($context);
  227. } catch (Exception $e) {
  228. while (ob_get_level() > $level) {
  229. ob_end_clean();
  230. }
  231. throw $e;
  232. }
  233. return ob_get_clean();
  234. }
  235. /**
  236. * Auto-generated method to display the template with the given context.
  237. *
  238. * @param array $context An array of parameters to pass to the template
  239. * @param array $blocks An array of blocks to pass to the template
  240. */
  241. abstract protected function doDisplay(array $context, array $blocks = array());
  242. /**
  243. * Returns a variable from the context.
  244. *
  245. * @param array $context The context
  246. * @param string $item The variable to return from the context
  247. * @param Boolean $ignoreStrictCheck Whether to ignore the strict variable check or not
  248. *
  249. * @return The content of the context variable
  250. *
  251. * @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
  252. */
  253. protected function getContext($context, $item, $ignoreStrictCheck = false)
  254. {
  255. if (!array_key_exists($item, $context)) {
  256. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  257. return null;
  258. }
  259. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
  260. }
  261. return $context[$item];
  262. }
  263. /**
  264. * Returns the attribute value for a given array/object.
  265. *
  266. * @param mixed $object The object or array from where to get the item
  267. * @param mixed $item The item to get from the array or object
  268. * @param array $arguments An array of arguments to pass if the item is an object method
  269. * @param string $type The type of attribute (@see Twig_TemplateInterface)
  270. * @param Boolean $isDefinedTest Whether this is only a defined check
  271. * @param Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not
  272. *
  273. * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
  274. *
  275. * @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
  276. */
  277. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
  278. {
  279. $item = (string) $item;
  280. // array
  281. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  282. if ((is_array($object) && array_key_exists($item, $object))
  283. || ($object instanceof ArrayAccess && isset($object[$item]))
  284. ) {
  285. if ($isDefinedTest) {
  286. return true;
  287. }
  288. return $object[$item];
  289. }
  290. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  291. if ($isDefinedTest) {
  292. return false;
  293. }
  294. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  295. return null;
  296. }
  297. if (is_object($object)) {
  298. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  299. // array
  300. } else {
  301. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  302. }
  303. }
  304. }
  305. if (!is_object($object)) {
  306. if ($isDefinedTest) {
  307. return false;
  308. }
  309. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  310. return null;
  311. }
  312. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object));
  313. }
  314. $class = get_class($object);
  315. // object property
  316. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  317. /* apparently, this is not needed as this is already covered by the array_key_exists() call below
  318. if (!isset(self::$cache[$class]['properties'])) {
  319. foreach (get_object_vars($object) as $k => $v) {
  320. self::$cache[$class]['properties'][$k] = true;
  321. }
  322. }
  323. */
  324. if (isset($object->$item) || array_key_exists($item, $object)) {
  325. if ($isDefinedTest) {
  326. return true;
  327. }
  328. if ($this->env->hasExtension('sandbox')) {
  329. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  330. }
  331. return $object->$item;
  332. }
  333. }
  334. // object method
  335. if (!isset(self::$cache[$class]['methods'])) {
  336. self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
  337. }
  338. $lcItem = strtolower($item);
  339. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  340. $method = $item;
  341. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  342. $method = 'get'.$item;
  343. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  344. $method = 'is'.$item;
  345. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  346. $method = $item;
  347. } else {
  348. if ($isDefinedTest) {
  349. return false;
  350. }
  351. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  352. return null;
  353. }
  354. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  355. }
  356. if ($isDefinedTest) {
  357. return true;
  358. }
  359. if ($this->env->hasExtension('sandbox')) {
  360. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  361. }
  362. $ret = call_user_func_array(array($object, $method), $arguments);
  363. if ($object instanceof Twig_TemplateInterface) {
  364. return new Twig_Markup($ret);
  365. }
  366. return $ret;
  367. }
  368. /**
  369. * This method is only useful when testing Twig. Do not use it.
  370. */
  371. static public function clearCache()
  372. {
  373. self::$cache = array();
  374. }
  375. }