smarty_internal_runtime_inheritance.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. */
  93. public function endChild()
  94. {
  95. $this->inheritanceLevel --;
  96. if (!$this->inheritanceLevel) {
  97. ob_end_clean();
  98. $this->state = 2;
  99. }
  100. }
  101. /**
  102. * Smarty_Internal_Block constructor.
  103. * - if outer level {block} of child template ($state == 1) save it as child root block
  104. * - otherwise process inheritance and render
  105. *
  106. * @param \Smarty_Internal_Template $tpl
  107. * @param $className
  108. * @param string $name
  109. * @param int|null $tplIndex index of outer level {block} if nested
  110. */
  111. public function instanceBlock(Smarty_Internal_Template $tpl, $className, $name, $tplIndex = null)
  112. {
  113. $block = new $className($name, $tplIndex ? $tplIndex : $this->tplIndex);
  114. if (isset($this->childRoot[ $name ])) {
  115. $block->child = $this->childRoot[ $name ];
  116. }
  117. if ($this->state == 1) {
  118. $this->childRoot[ $name ] = $block;
  119. return;
  120. }
  121. // make sure we got child block of child template of current block
  122. while ($block->child && $block->tplIndex <= $block->child->tplIndex) {
  123. $block->child = $block->child->child;
  124. }
  125. $this->process($tpl, $block);
  126. }
  127. /**
  128. * Goto child block or render this
  129. *
  130. * @param \Smarty_Internal_Template $tpl
  131. * @param \Smarty_Internal_Block $block
  132. * @param \Smarty_Internal_Block|null $parent
  133. *
  134. * @throws \SmartyException
  135. */
  136. public function process(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block,
  137. Smarty_Internal_Block $parent = null)
  138. {
  139. if ($block->hide && !isset($block->child)) {
  140. return;
  141. }
  142. if (isset($block->child) && $block->child->hide && !isset($block->child->child)) {
  143. $block->child = null;
  144. }
  145. $block->parent = $parent;
  146. if ($block->append && !$block->prepend && isset($parent)) {
  147. $this->callParent($tpl, $block);
  148. }
  149. if ($block->callsChild || !isset($block->child) || ($block->child->hide && !isset($block->child->child))) {
  150. $this->callBlock($block, $tpl);
  151. } else {
  152. $this->process($tpl, $block->child, $block);
  153. }
  154. if ($block->prepend && isset($parent)) {
  155. $this->callParent($tpl, $block);
  156. if ($block->append) {
  157. if ($block->callsChild || !isset($block->child) ||
  158. ($block->child->hide && !isset($block->child->child))
  159. ) {
  160. $this->callBlock($block, $tpl);
  161. } else {
  162. $this->process($tpl, $block->child, $block);
  163. }
  164. }
  165. }
  166. $block->parent = null;
  167. }
  168. /**
  169. * Render child on {$smarty.block.child}
  170. *
  171. * @param \Smarty_Internal_Template $tpl
  172. * @param \Smarty_Internal_Block $block
  173. */
  174. public function callChild(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block)
  175. {
  176. if (isset($block->child)) {
  177. $this->process($tpl, $block->child, $block);
  178. }
  179. }
  180. /**
  181. * Render parent on {$smarty.block.parent} or {block append/prepend} *
  182. *
  183. * @param \Smarty_Internal_Template $tpl
  184. * @param \Smarty_Internal_Block $block
  185. *
  186. * @throws \SmartyException
  187. */
  188. public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block)
  189. {
  190. if (isset($block->parent)) {
  191. $this->callBlock($block->parent, $tpl);
  192. } else {
  193. 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}'");
  194. }
  195. }
  196. /**
  197. * @param \Smarty_Internal_Block $block
  198. * @param \Smarty_Internal_Template $tpl
  199. */
  200. public function callBlock(Smarty_Internal_Block $block, Smarty_Internal_Template $tpl)
  201. {
  202. $this->sourceStack[] = $tpl->source;
  203. $tpl->source = $this->sources[ $block->tplIndex ];
  204. $block->callBlock($tpl);
  205. $tpl->source = array_pop($this->sourceStack);
  206. }
  207. }