You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_inheritance.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Inheritance Runtime Methods processBlock, endChild, init
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_Inheritance
  11. {
  12. /**
  13. * State machine
  14. * - 0 idle next extends will create a new inheritance tree
  15. * - 1 processing child template
  16. * - 2 wait for next inheritance template
  17. * - 3 assume parent template, if child will loaded goto state 1
  18. * a call to a sub template resets the state to 0
  19. *
  20. * @var int
  21. */
  22. public $state = 0;
  23. /**
  24. * Array of root child {block} objects
  25. *
  26. * @var Smarty_Internal_Block[]
  27. */
  28. public $childRoot = array();
  29. /**
  30. * inheritance template nesting level
  31. *
  32. * @var int
  33. */
  34. public $inheritanceLevel = 0;
  35. /**
  36. * inheritance template index
  37. *
  38. * @var int
  39. */
  40. public $tplIndex = - 1;
  41. /**
  42. * Array of template source objects
  43. *
  44. * @var Smarty_Template_Source[]
  45. */
  46. public $sources = array();
  47. /**
  48. * Stack of source objects while executing block code
  49. *
  50. * @var Smarty_Template_Source[]
  51. */
  52. public $sourceStack = array();
  53. /**
  54. * Initialize inheritance
  55. *
  56. * @param \Smarty_Internal_Template $tpl template object of caller
  57. * @param bool $initChild if true init for child template
  58. * @param array $blockNames outer level block name
  59. *
  60. */
  61. public function init(Smarty_Internal_Template $tpl, $initChild, $blockNames = array())
  62. {
  63. // if called while executing parent template it must be a sub-template with new inheritance root
  64. if ($initChild && $this->state === 3 && (strpos($tpl->template_resource, 'extendsall') === false)) {
  65. $tpl->inheritance = new Smarty_Internal_Runtime_Inheritance();
  66. $tpl->inheritance->init($tpl, $initChild, $blockNames);
  67. return;
  68. }
  69. ++$this->tplIndex;
  70. $this->sources[ $this->tplIndex ] = $tpl->source;
  71. // start of child sub template(s)
  72. if ($initChild) {
  73. $this->state = 1;
  74. if (!$this->inheritanceLevel) {
  75. //grab any output of child templates
  76. ob_start();
  77. }
  78. ++$this->inheritanceLevel;
  79. // $tpl->startRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateStart');
  80. // $tpl->endRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateEnd');
  81. }
  82. // if state was waiting for parent change state to parent
  83. if ($this->state === 2) {
  84. $this->state = 3;
  85. }
  86. }
  87. /**
  88. * End of child template(s)
  89. * - if outer level is reached flush output buffer and switch to wait for parent template state
  90. *
  91. * @param \Smarty_Internal_Template $tpl
  92. * @param null|string $template optional name of inheritance parent template
  93. * @param null|string $uid uid of inline template
  94. * @param null|string $func function call name of inline template
  95. *
  96. * @throws \Exception
  97. * @throws \SmartyException
  98. */
  99. public function endChild(Smarty_Internal_Template $tpl, $template = null, $uid = null, $func = null)
  100. {
  101. --$this->inheritanceLevel;
  102. if (!$this->inheritanceLevel) {
  103. ob_end_clean();
  104. $this->state = 2;
  105. }
  106. if (isset($template) && (($tpl->parent->_isTplObj() && $tpl->parent->source->type !== 'extends') ||
  107. $tpl->smarty->extends_recursion)) {
  108. $tpl->_subTemplateRender($template,
  109. $tpl->cache_id,
  110. $tpl->compile_id,
  111. $tpl->caching ? 9999 : 0,
  112. $tpl->cache_lifetime,
  113. array(),
  114. 2,
  115. false,
  116. $uid,
  117. $func);
  118. }
  119. }
  120. /**
  121. * Smarty_Internal_Block constructor.
  122. * - if outer level {block} of child template ($state === 1) save it as child root block
  123. * - otherwise process inheritance and render
  124. *
  125. * @param \Smarty_Internal_Template $tpl
  126. * @param $className
  127. * @param string $name
  128. * @param int|null $tplIndex index of outer level {block} if nested
  129. *
  130. * @throws \SmartyException
  131. */
  132. public function instanceBlock(Smarty_Internal_Template $tpl, $className, $name, $tplIndex = null)
  133. {
  134. $block = new $className($name, isset($tplIndex) ? $tplIndex : $this->tplIndex);
  135. if (isset($this->childRoot[ $name ])) {
  136. $block->child = $this->childRoot[ $name ];
  137. }
  138. if ($this->state === 1) {
  139. $this->childRoot[ $name ] = $block;
  140. return;
  141. }
  142. // make sure we got child block of child template of current block
  143. while ($block->child && $block->tplIndex <= $block->child->tplIndex) {
  144. $block->child = $block->child->child;
  145. }
  146. $this->process($tpl, $block);
  147. }
  148. /**
  149. * Goto child block or render this
  150. *
  151. * @param \Smarty_Internal_Template $tpl
  152. * @param \Smarty_Internal_Block $block
  153. * @param \Smarty_Internal_Block|null $parent
  154. *
  155. * @throws \SmartyException
  156. */
  157. public function process(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block,
  158. Smarty_Internal_Block $parent = null)
  159. {
  160. if ($block->hide && !isset($block->child)) {
  161. return;
  162. }
  163. if (isset($block->child) && $block->child->hide && !isset($block->child->child)) {
  164. $block->child = null;
  165. }
  166. $block->parent = $parent;
  167. if ($block->append && !$block->prepend && isset($parent)) {
  168. $this->callParent($tpl, $block, '\'{block append}\'');
  169. }
  170. if ($block->callsChild || !isset($block->child) || ($block->child->hide && !isset($block->child->child))) {
  171. $this->callBlock($block, $tpl);
  172. } else {
  173. $this->process($tpl, $block->child, $block);
  174. }
  175. if ($block->prepend && isset($parent)) {
  176. $this->callParent($tpl, $block, '{block prepend}');
  177. if ($block->append) {
  178. if ($block->callsChild || !isset($block->child) ||
  179. ($block->child->hide && !isset($block->child->child))
  180. ) {
  181. $this->callBlock($block, $tpl);
  182. } else {
  183. $this->process($tpl, $block->child, $block);
  184. }
  185. }
  186. }
  187. $block->parent = null;
  188. }
  189. /**
  190. * Render child on \$smarty.block.child
  191. *
  192. * @param \Smarty_Internal_Template $tpl
  193. * @param \Smarty_Internal_Block $block
  194. *
  195. * @return null|string block content
  196. * @throws \SmartyException
  197. */
  198. public function callChild(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block)
  199. {
  200. if (isset($block->child)) {
  201. $this->process($tpl, $block->child, $block);
  202. }
  203. }
  204. /**
  205. * Render parent block on \$smarty.block.parent or {block append/prepend}
  206. *
  207. * @param \Smarty_Internal_Template $tpl
  208. * @param \Smarty_Internal_Block $block
  209. * @param string $tag
  210. *
  211. * @return null|string block content
  212. * @throws \SmartyException
  213. */
  214. public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block, $tag)
  215. {
  216. if (isset($block->parent)) {
  217. $this->callBlock($block->parent, $tpl);
  218. } else {
  219. throw new SmartyException("inheritance: illegal '{$tag}' used in child template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'");
  220. }
  221. }
  222. /**
  223. * render block
  224. *
  225. * @param \Smarty_Internal_Block $block
  226. * @param \Smarty_Internal_Template $tpl
  227. */
  228. public function callBlock(Smarty_Internal_Block $block, Smarty_Internal_Template $tpl)
  229. {
  230. $this->sourceStack[] = $tpl->source;
  231. $tpl->source = $this->sources[ $block->tplIndex ];
  232. $block->callBlock($tpl);
  233. $tpl->source = array_pop($this->sourceStack);
  234. }
  235. }