smarty_internal_runtime_inheritance.php 8.3 KB

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