您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

smarty_internal_runtime_inheritance.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 block parameter of known {block} tags
  25. *
  26. * @var array
  27. */
  28. public $blockParameter = 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 compiled template file path
  43. * - key template index
  44. * only used when caching is enabled
  45. *
  46. * @var []string
  47. */
  48. public $compiledFilePath = array();
  49. /**
  50. * Current {block} nesting level
  51. *
  52. * @var int
  53. */
  54. public $blockNesting = 0;
  55. /**
  56. * Initialize inheritance
  57. *
  58. * @param \Smarty_Internal_Template $tpl template object of caller
  59. * @param bool $initChild if true init for child template
  60. * @param array $blockNames outer level block name
  61. *
  62. */
  63. public function init(Smarty_Internal_Template $tpl, $initChild, $blockNames = array())
  64. {
  65. // if template was from an inner block or template is a parent template create new inheritance root
  66. if ($initChild && ($this->blockNesting || $this->state == 3)) {
  67. $tpl->ext->_inheritance = new Smarty_Internal_Runtime_Inheritance();
  68. $tpl->ext->_inheritance->init($tpl, $initChild, $blockNames);
  69. return;
  70. }
  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. }
  80. // in parent state {include} will not increment template index
  81. if ($this->state != 3) {
  82. $this->tplIndex ++;
  83. }
  84. // if state was waiting for parent change state to parent
  85. if ($this->state == 2) {
  86. $this->state = 3;
  87. }
  88. }
  89. /**
  90. * End of child template(s)
  91. * - if outer level is reached flush output buffer and switch to wait for parent template state
  92. *
  93. * @param \Smarty_Internal_Template $tpl template object of caller
  94. */
  95. public function endChild(Smarty_Internal_Template $tpl)
  96. {
  97. $this->inheritanceLevel --;
  98. if (!$this->inheritanceLevel) {
  99. ob_end_clean();
  100. $this->state = 2;
  101. }
  102. }
  103. /**
  104. * Process inheritance {block} tag
  105. *
  106. * $type 0 = {block}:
  107. * - search in inheritance template hierarchy for child blocks
  108. * if found call it, otherwise call current block
  109. * - ignored for outer level blocks in child templates
  110. *
  111. * $type 1 = {block}:
  112. * - nested {block}
  113. * - search in inheritance template hierarchy for child blocks
  114. * if found call it, otherwise call current block
  115. *
  116. * $type 2 = {$smarty.block.child}:
  117. * - search in inheritance template hierarchy for child blocks
  118. * if found call it, otherwise ignore
  119. *
  120. * $type 3 = {block append} {block prepend}:
  121. * - call parent block
  122. *
  123. * $type 4 = {$smarty.block.parent}:
  124. * - call parent block
  125. *
  126. * @param \Smarty_Internal_Template $tpl template object of caller
  127. * @param int $type call type see above
  128. * @param string $name block name
  129. * @param array $block block parameter
  130. * @param array $callStack call stack with block parameters
  131. *
  132. * @throws \SmartyException
  133. */
  134. public function processBlock(Smarty_Internal_Template $tpl, $type = 0, $name, $block, $callStack = array())
  135. {
  136. if (!isset($this->blockParameter[ $name ])) {
  137. $this->blockParameter[ $name ] = array();
  138. }
  139. if ($this->state == 1) {
  140. $block[ 2 ] = count($this->blockParameter[ $name ]);
  141. $block[ 3 ] = $this->tplIndex;
  142. $this->blockParameter[ $name ][] = $block;
  143. return;
  144. }
  145. if ($type == 3) {
  146. if (!empty($callStack)) {
  147. $block = array_shift($callStack);
  148. } else {
  149. return;
  150. }
  151. } elseif ($type == 4) {
  152. if (!empty($callStack)) {
  153. array_shift($callStack);
  154. if (empty($callStack)) {
  155. throw new SmartyException("inheritance: tag {\$smarty.block.parent} used in parent template block '{$name}'");
  156. }
  157. $block = array_shift($callStack);
  158. } else {
  159. return;
  160. }
  161. } else {
  162. $index = 0;
  163. $blockParameter = &$this->blockParameter[ $name ];
  164. if ($type == 0) {
  165. $index = $block[ 2 ] = count($blockParameter);
  166. $block[ 3 ] = $this->tplIndex;
  167. $callStack = array(&$block);
  168. } elseif ($type == 1) {
  169. $block[ 3 ] = $callStack[ 0 ][ 3 ];
  170. for ($i = 0; $i < count($blockParameter); $i ++) {
  171. if ($blockParameter[ $i ][ 3 ] <= $block[ 3 ]) {
  172. $index = $blockParameter[ $i ][ 2 ];
  173. }
  174. }
  175. $block[ 2 ] = $index;
  176. $callStack = array(&$block);
  177. } elseif ($type == 2) {
  178. $index = $callStack[ 0 ][ 2 ];
  179. if ($index == 0) {
  180. return;
  181. }
  182. $callStack = $block = array(1 => false);
  183. }
  184. $index --;
  185. // find lowest level child block
  186. while ($index >= 0 && ($type || !$block[ 1 ])) {
  187. $block = &$blockParameter[ $index ];
  188. array_unshift($callStack, $block);
  189. if ($block[ 1 ]) {
  190. break;
  191. }
  192. $index --;
  193. }
  194. if (isset($block[ 'hide' ]) && $index <= 0) {
  195. return;
  196. }
  197. }
  198. $this->blockNesting ++;
  199. // {block append} ?
  200. if (isset($block[ 'append' ])) {
  201. $appendStack = $callStack;
  202. if ($type == 0) {
  203. array_shift($appendStack);
  204. }
  205. $this->processBlock($tpl, 3, $name, null, $appendStack);
  206. }
  207. // call block of current stack level
  208. if (isset($block[6])) {
  209. $block[6]($tpl, $callStack);
  210. } else {
  211. $block[0]($tpl, $callStack);
  212. }
  213. // {block prepend} ?
  214. if (isset($block[ 'prepend' ])) {
  215. $prependStack = $callStack;
  216. if ($type == 0) {
  217. array_shift($prependStack);
  218. }
  219. $this->processBlock($tpl, 3, $name, null, $prependStack);
  220. }
  221. $this->blockNesting --;
  222. }
  223. }