smarty_internal_compile_block.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Block
  4. *
  5. * Compiles the {block}{/block} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Block Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name', 'hide');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('hide');
  39. /**
  40. * Compiles code for the {block} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. * @return boolean true
  45. */
  46. public function compile($args, $compiler) {
  47. // check and get attributes
  48. $_attr = $this->getAttributes($compiler, $args);
  49. $save = array($_attr, $compiler->parser->current_buffer, $compiler->nocache, $compiler->smarty->merge_compiled_includes, $compiler->merged_templates, $compiler->smarty->merged_templates_func, $compiler->template->properties, $compiler->template->has_nocache_code);
  50. $this->openTag($compiler, 'block', $save);
  51. if ($_attr['nocache'] == true) {
  52. $compiler->nocache = true;
  53. }
  54. // set flag for {block} tag
  55. $compiler->inheritance = true;
  56. // must merge includes
  57. $compiler->smarty->merge_compiled_includes = true;
  58. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  59. $compiler->has_code = false;
  60. return true;
  61. }
  62. /**
  63. * Save or replace child block source by block name during parsing
  64. *
  65. * @param string $block_content block source content
  66. * @param string $block_tag opening block tag
  67. * @param object $template template object
  68. * @param string $filepath filepath of template source
  69. */
  70. public static function saveBlockData($block_content, $block_tag, $template, $filepath) {
  71. $_rdl = preg_quote($template->smarty->right_delimiter);
  72. $_ldl = preg_quote($template->smarty->left_delimiter);
  73. if ($template->smarty->auto_literal) {
  74. $al = '\s*';
  75. } else {
  76. $al = '';
  77. }
  78. if (0 == preg_match("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)?(\s*)?(hide)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
  79. $error_text = 'Syntax Error in template "' . $template->source->filepath . '" "' . htmlspecialchars($block_tag) . '" illegal options';
  80. throw new SmartyCompilerException($error_text);
  81. } else {
  82. $_name = trim($_match[3], '\'"');
  83. if ($_match[8] != 'hide' || isset($template->block_data[$_name])) { // replace {$smarty.block.child}
  84. // do we have {$smart.block.child} in nested {block} tags?
  85. if (0 != preg_match_all("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")([\s\S]*?)(hide)?(\s*{$_rdl})([\s\S]*?)({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})([\s\S]*?{$_ldl}{$al}/block{$_rdl})!", $block_content, $_match2)) {
  86. foreach ($_match2[3] as $key => $name) {
  87. // get it's replacement
  88. $_name2 = trim($name, '\'"');
  89. if ($_match2[5][$key] != 'hide' || isset($template->block_data[$_name2])) {
  90. if (isset($template->block_data[$_name2])) {
  91. $replacement = $template->block_data[$_name2]['source'];
  92. } else {
  93. $replacement = '';
  94. }
  95. // replace {$smarty.block.child} tag
  96. $search = array("%({$_ldl}{$al}block[\s\S]*?{$name}[\s\S]*?{$_rdl})([\s\S]*?)({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})([\s\S]*?)({$_ldl}{$al}/block{$_rdl})%", "/§§§child§§§/");
  97. $replace = array('\2§§§child§§§\4', $replacement);
  98. $block_content = preg_replace($search, $replace, $block_content);
  99. } else {
  100. // remove hidden blocks
  101. $block_content = preg_replace("%({$_ldl}{$al}block[\s\S]*?{$name}[\s\S]*?{$_rdl}[\s\S]*?{$_ldl}{$al}/block{$_rdl})%", '', $block_content);
  102. }
  103. }
  104. }
  105. // do we have not nested {$smart.block.child}
  106. if (0 != preg_match("/({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})/", $block_content, $_match2)) {
  107. // get child replacement for this block
  108. if (isset($template->block_data[$_name])) {
  109. $replacement = $template->block_data[$_name]['source'];
  110. unset($template->block_data[$_name]);
  111. } else {
  112. $replacement = '';
  113. }
  114. $block_content = preg_replace("/({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})/", $replacement, $block_content);
  115. }
  116. if (isset($template->block_data[$_name])) {
  117. if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  118. $template->block_data[$_name]['source'] =
  119. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
  120. } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
  121. $template->block_data[$_name]['source'] .= $block_content;
  122. } elseif ($template->block_data[$_name]['mode'] == 'append') {
  123. $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
  124. }
  125. } else {
  126. $template->block_data[$_name]['source'] = $block_content;
  127. $template->block_data[$_name]['file'] = $filepath;
  128. }
  129. if ($_match[6] == 'append') {
  130. $template->block_data[$_name]['mode'] = 'append';
  131. } elseif ($_match[6] == 'prepend') {
  132. $template->block_data[$_name]['mode'] = 'prepend';
  133. } else {
  134. $template->block_data[$_name]['mode'] = 'replace';
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * Compile saved child block source
  141. *
  142. * @param object $compiler compiler object
  143. * @param string $_name optional name of child block
  144. * @return string compiled code of schild block
  145. */
  146. public static function compileChildBlock($compiler, $_name = null) {
  147. $_output = '';
  148. // if called by {$smarty.block.child} we must search the name of enclosing {block}
  149. if ($_name == null) {
  150. $stack_count = count($compiler->_tag_stack);
  151. while (--$stack_count >= 0) {
  152. if ($compiler->_tag_stack[$stack_count][0] == 'block') {
  153. $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'], "'\"");
  154. break;
  155. }
  156. }
  157. // flag that child is already compile by {$smarty.block.child} inclusion
  158. $compiler->template->block_data[$_name]['compiled'] = true;
  159. }
  160. if ($_name == null) {
  161. $compiler->trigger_template_error('{$smarty.block.child} used out of context', $compiler->lex->taglineno);
  162. }
  163. // undefined child?
  164. if (!isset($compiler->template->block_data[$_name]['source'])) {
  165. return '';
  166. }
  167. $_tpl = new Smarty_Internal_template('string:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
  168. $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
  169. $_tpl->variable_filters = $compiler->template->variable_filters;
  170. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  171. $_tpl->source->filepath = $compiler->template->block_data[$_name]['file'];
  172. $_tpl->allow_relative_path = true;
  173. if ($compiler->nocache) {
  174. $_tpl->compiler->forceNocache = 2;
  175. } else {
  176. $_tpl->compiler->forceNocache = 1;
  177. }
  178. $_tpl->compiler->suppressHeader = true;
  179. $_tpl->compiler->suppressTemplatePropertyHeader = true;
  180. $_tpl->compiler->suppressMergedTemplates = true;
  181. if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  182. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl));
  183. } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
  184. $_output = $_tpl->compiler->compileTemplate($_tpl) . $compiler->parser->current_buffer->to_smarty_php();
  185. } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
  186. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl);
  187. } elseif (!empty($compiler->template->block_data[$_name])) {
  188. $_output = $_tpl->compiler->compileTemplate($_tpl);
  189. }
  190. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  191. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  192. $compiler->merged_templates = array_merge($compiler->merged_templates, $_tpl->compiler->merged_templates);
  193. $compiler->template->variable_filters = $_tpl->variable_filters;
  194. if ($_tpl->has_nocache_code) {
  195. $compiler->template->has_nocache_code = true;
  196. }
  197. foreach ($_tpl->required_plugins as $key => $tmp1) {
  198. if ($compiler->nocache && $compiler->template->caching) {
  199. $code = 'nocache';
  200. } else {
  201. $code = $key;
  202. }
  203. foreach ($tmp1 as $name => $tmp) {
  204. foreach ($tmp as $type => $data) {
  205. $compiler->template->required_plugins[$code][$name][$type] = $data;
  206. }
  207. }
  208. }
  209. unset($_tpl);
  210. return $_output;
  211. }
  212. }
  213. /**
  214. * Smarty Internal Plugin Compile BlockClose Class
  215. *
  216. * @package Smarty
  217. * @subpackage Compiler
  218. */
  219. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  220. /**
  221. * Compiles code for the {/block} tag
  222. *
  223. * @param array $args array with attributes from parser
  224. * @param object $compiler compiler object
  225. * @return string compiled code
  226. */
  227. public function compile($args, $compiler) {
  228. $compiler->has_code = true;
  229. // check and get attributes
  230. $_attr = $this->getAttributes($compiler, $args);
  231. $saved_data = $this->closeTag($compiler, array('block'));
  232. $_name = trim($saved_data[0]['name'], "\"'");
  233. if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
  234. // restore to status before {block} tag as new subtemplate code of parent {block} is not needed
  235. // TODO: Below code was disabled in 3.1.8 because of problems with {include} in nested {block} tags in child templates
  236. // combined with append/prepend or $smarty.block.parent
  237. // For later versions it should be checked under which conditions it could run for optimisation
  238. //
  239. //$compiler->merged_templates = $saved_data[4];
  240. //$compiler->smarty->merged_templates_func = $saved_data[5];
  241. //$compiler->template->properties = $saved_data[6];
  242. //$compiler->template->has_nocache_code = $saved_data[7];
  243. $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
  244. } else {
  245. if (isset($saved_data[0]['hide']) && !isset($compiler->template->block_data[$_name]['source'])) {
  246. $_output = '';
  247. } else {
  248. $_output = $compiler->parser->current_buffer->to_smarty_php();
  249. }
  250. unset($compiler->template->block_data[$_name]['compiled']);
  251. }
  252. // reset flags
  253. $compiler->parser->current_buffer = $saved_data[1];
  254. $compiler->nocache = $saved_data[2];
  255. $compiler->smarty->merge_compiled_includes = $saved_data[3];
  256. // reset flag for {block} tag
  257. $compiler->inheritance = false;
  258. // $_output content has already nocache code processed
  259. $compiler->suppressNocacheProcessing = true;
  260. return $_output;
  261. }
  262. }
  263. ?>