smarty_internal_compile_block.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. {
  48. // check and get attributes
  49. $_attr = $this->getAttributes($compiler, $args);
  50. $save = array($_attr, $compiler->parser->current_buffer, $compiler->nocache, $compiler->smarty->merge_compiled_includes);
  51. $this->openTag($compiler, 'block', $save);
  52. if ($_attr['nocache'] == true) {
  53. $compiler->nocache = true;
  54. }
  55. // set flag for {block} tag
  56. $compiler->inheritance = true;
  57. // must merge includes
  58. $compiler->smarty->merge_compiled_includes = true;
  59. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  60. $compiler->has_code = false;
  61. return true;
  62. }
  63. /**
  64. * Save or replace child block source by block name during parsing
  65. *
  66. * @param string $block_content block source content
  67. * @param string $block_tag opening block tag
  68. * @param object $template template object
  69. * @param string $filepath filepath of template source
  70. */
  71. public static function saveBlockData($block_content, $block_tag, $template, $filepath)
  72. {
  73. $_rdl = preg_quote($template->smarty->right_delimiter);
  74. $_ldl = preg_quote($template->smarty->left_delimiter);
  75. if (0 == preg_match("!({$_ldl}block\s+)(name=)?(\w+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)?(\s*)?(hide)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
  76. $error_text = 'Syntax Error in template "' . $template->source->filepath . '" "' . htmlspecialchars($block_tag) . '" illegal options';
  77. throw new SmartyCompilerException($error_text);
  78. } else {
  79. $_name = trim($_match[3], '\'"');
  80. if ($_match[8] != 'hide' || isset($template->block_data[$_name])) { // replace {$smarty.block.child}
  81. if (strpos($block_content, $template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter) !== false) {
  82. if (isset($template->block_data[$_name])) {
  83. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  84. $template->block_data[$_name]['source'], $block_content);
  85. unset($template->block_data[$_name]);
  86. } else {
  87. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  88. '', $block_content);
  89. }
  90. }
  91. if (isset($template->block_data[$_name])) {
  92. if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  93. $template->block_data[$_name]['source'] =
  94. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
  95. } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
  96. $template->block_data[$_name]['source'] .= $block_content;
  97. } elseif ($template->block_data[$_name]['mode'] == 'append') {
  98. $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
  99. }
  100. } else {
  101. $template->block_data[$_name]['source'] = $block_content;
  102. $template->block_data[$_name]['file'] = $filepath;
  103. }
  104. if ($_match[6] == 'append') {
  105. $template->block_data[$_name]['mode'] = 'append';
  106. } elseif ($_match[6] == 'prepend') {
  107. $template->block_data[$_name]['mode'] = 'prepend';
  108. } else {
  109. $template->block_data[$_name]['mode'] = 'replace';
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Compile saved child block source
  116. *
  117. * @param object $compiler compiler object
  118. * @param string $_name optional name of child block
  119. * @return string compiled code of schild block
  120. */
  121. public static function compileChildBlock($compiler, $_name = null)
  122. {
  123. $_output = '';
  124. // if called by {$smarty.block.child} we must search the name of enclosing {block}
  125. if ($_name == null) {
  126. $stack_count = count($compiler->_tag_stack);
  127. while (--$stack_count >= 0) {
  128. if ($compiler->_tag_stack[$stack_count][0] == 'block') {
  129. $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'] ,"'\"");
  130. break;
  131. }
  132. }
  133. // flag that child is already compile by {$smarty.block.child} inclusion
  134. $compiler->template->block_data[$_name]['compiled'] = true;
  135. }
  136. if ($_name == null) {
  137. $compiler->trigger_template_error('{$smarty.block.child} used out of context', $compiler->lex->taglineno);
  138. }
  139. // undefined child?
  140. if (!isset($compiler->template->block_data[$_name]['source'])) {
  141. return '';
  142. }
  143. $_tpl = new Smarty_Internal_template ('string:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
  144. $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
  145. $_tpl->variable_filters = $compiler->template->variable_filters;
  146. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  147. $_tpl->source->filepath = $compiler->template->block_data[$_name]['file'];
  148. $_tpl->allow_relative_path = true;
  149. if ($compiler->nocache) {
  150. $_tpl->compiler->forceNocache = 2;
  151. } else {
  152. $_tpl->compiler->forceNocache = 1;
  153. }
  154. $_tpl->compiler->suppressHeader = true;
  155. $_tpl->compiler->suppressTemplatePropertyHeader = true;
  156. $_tpl->compiler->suppressMergedTemplates = true;
  157. if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  158. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl));
  159. } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
  160. $_output = $_tpl->compiler->compileTemplate($_tpl) . $compiler->parser->current_buffer->to_smarty_php();
  161. } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
  162. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl);
  163. } elseif (!empty($compiler->template->block_data[$_name])) {
  164. $_output = $_tpl->compiler->compileTemplate($_tpl);
  165. }
  166. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  167. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  168. $compiler->merged_templates = array_merge($compiler->merged_templates, $_tpl->compiler->merged_templates);
  169. $compiler->template->variable_filters = $_tpl->variable_filters;
  170. if ($_tpl->has_nocache_code) {
  171. $compiler->template->has_nocache_code = true;
  172. }
  173. foreach($_tpl->required_plugins as $code => $tmp1) {
  174. foreach($tmp1 as $name => $tmp) {
  175. foreach($tmp as $type => $data) {
  176. $compiler->template->required_plugins[$code][$name][$type] = $data;
  177. }
  178. }
  179. }
  180. unset($_tpl);
  181. return $_output;
  182. }
  183. }
  184. /**
  185. * Smarty Internal Plugin Compile BlockClose Class
  186. *
  187. * @package Smarty
  188. * @subpackage Compiler
  189. */
  190. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  191. /**
  192. * Compiles code for the {/block} tag
  193. *
  194. * @param array $args array with attributes from parser
  195. * @param object $compiler compiler object
  196. * @return string compiled code
  197. */
  198. public function compile($args, $compiler)
  199. {
  200. $compiler->has_code = true;
  201. // check and get attributes
  202. $_attr = $this->getAttributes($compiler, $args);
  203. $saved_data = $this->closeTag($compiler, array('block'));
  204. $_name = trim($saved_data[0]['name'], "\"'");
  205. if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
  206. $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
  207. } else {
  208. if (isset($saved_data[0]['hide']) && !isset($compiler->template->block_data[$_name]['source'])) {
  209. $_output = '';
  210. } else {
  211. $_output = $compiler->parser->current_buffer->to_smarty_php();
  212. }
  213. unset ($compiler->template->block_data[$_name]['compiled']);
  214. }
  215. // reset flags
  216. $compiler->parser->current_buffer = $saved_data[1];
  217. $compiler->nocache = $saved_data[2];
  218. $compiler->smarty->merge_compiled_includes = $saved_data[3];
  219. // reset flag for {block} tag
  220. $compiler->inheritance = false;
  221. // $_output content has already nocache code processed
  222. $compiler->suppressNocacheProcessing = true;
  223. return $_output;
  224. }
  225. }
  226. ?>