4 * Smarty Internal Plugin Compile Block
6 * Compiles the {block}{/block} tags
14 * Smarty Internal Plugin Compile Block Class
17 * @subpackage Compiler
19 class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
22 * Attribute definition: Overwrites base class.
25 * @see Smarty_Internal_CompileBase
27 public $required_attributes = array('name');
30 * Attribute definition: Overwrites base class.
33 * @see Smarty_Internal_CompileBase
35 public $shorttag_order = array('name', 'hide');
38 * Attribute definition: Overwrites base class.
41 * @see Smarty_Internal_CompileBase
43 public $optional_attributes = array('hide');
46 * Compiles code for the {block} tag
48 * @param array $args array with attributes from parser
49 * @param object $compiler compiler object
50 * @return boolean true
52 public function compile($args, $compiler) {
53 // check and get attributes
54 $_attr = $this->getAttributes($compiler, $args);
55 $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);
56 $this->openTag($compiler, 'block', $save);
57 if ($_attr['nocache'] == true) {
58 $compiler->nocache = true;
60 // set flag for {block} tag
61 $compiler->inheritance = true;
62 // must merge includes
63 $compiler->smarty->merge_compiled_includes = true;
65 $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
66 $compiler->has_code = false;
71 * Save or replace child block source by block name during parsing
73 * @param string $block_content block source content
74 * @param string $block_tag opening block tag
75 * @param object $template template object
76 * @param string $filepath filepath of template source
78 public static function saveBlockData($block_content, $block_tag, $template, $filepath) {
79 $_rdl = preg_quote($template->smarty->right_delimiter);
80 $_ldl = preg_quote($template->smarty->left_delimiter);
81 if ($template->smarty->auto_literal) {
86 if (0 == preg_match("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)?(\s*)?(hide)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
87 $error_text = 'Syntax Error in template "' . $template->source->filepath . '" "' . htmlspecialchars($block_tag) . '" illegal options';
88 throw new SmartyCompilerException($error_text);
90 $_name = trim($_match[3], '\'"');
91 if ($_match[8] != 'hide' || isset($template->block_data[$_name])) { // replace {$smarty.block.child}
92 // do we have {$smart.block.child} in nested {block} tags?
93 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)) {
94 foreach ($_match2[3] as $key => $name) {
95 // get it's replacement
96 $_name2 = trim($name, '\'"');
97 if ($_match2[5][$key] != 'hide' || isset($template->block_data[$_name2])) {
98 if (isset($template->block_data[$_name2])) {
99 $replacement = $template->block_data[$_name2]['source'];
103 // replace {$smarty.block.child} tag
104 $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§§§/");
105 $replace = array('\2§§§child§§§\4', $replacement);
106 $block_content = preg_replace($search, $replace, $block_content);
108 // remove hidden blocks
109 $block_content = preg_replace("%({$_ldl}{$al}block[\s\S]*?{$name}[\s\S]*?{$_rdl}[\s\S]*?{$_ldl}{$al}/block{$_rdl})%", '', $block_content);
113 // do we have not nested {$smart.block.child}
114 if (0 != preg_match("/({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})/", $block_content, $_match2)) {
115 // get child replacement for this block
116 if (isset($template->block_data[$_name])) {
117 $replacement = $template->block_data[$_name]['source'];
118 unset($template->block_data[$_name]);
122 $block_content = preg_replace("/({$_ldl}{$al}\\\$smarty\.block\.child{$_rdl})/", $replacement, $block_content);
124 if (isset($template->block_data[$_name])) {
125 if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
126 $template->block_data[$_name]['source'] =
127 str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
128 } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
129 $template->block_data[$_name]['source'] .= $block_content;
130 } elseif ($template->block_data[$_name]['mode'] == 'append') {
131 $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
134 $template->block_data[$_name]['source'] = $block_content;
135 $template->block_data[$_name]['file'] = $filepath;
137 if ($_match[6] == 'append') {
138 $template->block_data[$_name]['mode'] = 'append';
139 } elseif ($_match[6] == 'prepend') {
140 $template->block_data[$_name]['mode'] = 'prepend';
142 $template->block_data[$_name]['mode'] = 'replace';
149 * Compile saved child block source
151 * @param object $compiler compiler object
152 * @param string $_name optional name of child block
153 * @return string compiled code of schild block
155 public static function compileChildBlock($compiler, $_name = null) {
157 // if called by {$smarty.block.child} we must search the name of enclosing {block}
158 if ($_name == null) {
159 $stack_count = count($compiler->_tag_stack);
160 while (--$stack_count >= 0) {
161 if ($compiler->_tag_stack[$stack_count][0] == 'block') {
162 $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'], "'\"");
166 // flag that child is already compile by {$smarty.block.child} inclusion
167 $compiler->template->block_data[$_name]['compiled'] = true;
169 if ($_name == null) {
170 $compiler->trigger_template_error('{$smarty.block.child} used out of context', $compiler->lex->taglineno);
173 if (!isset($compiler->template->block_data[$_name]['source'])) {
176 $_tpl = new Smarty_Internal_template('string:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
177 $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
178 $_tpl->variable_filters = $compiler->template->variable_filters;
179 $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
180 $_tpl->source->filepath = $compiler->template->block_data[$_name]['file'];
181 $_tpl->allow_relative_path = true;
182 if ($compiler->nocache) {
183 $_tpl->compiler->forceNocache = 2;
185 $_tpl->compiler->forceNocache = 1;
187 $_tpl->compiler->suppressHeader = true;
188 $_tpl->compiler->suppressTemplatePropertyHeader = true;
189 $_tpl->compiler->suppressMergedTemplates = true;
190 if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
191 $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl));
192 } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
193 $_output = $_tpl->compiler->compileTemplate($_tpl) . $compiler->parser->current_buffer->to_smarty_php();
194 } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
195 $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl);
196 } elseif (!empty($compiler->template->block_data[$_name])) {
197 $_output = $_tpl->compiler->compileTemplate($_tpl);
199 $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
200 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
201 $compiler->merged_templates = array_merge($compiler->merged_templates, $_tpl->compiler->merged_templates);
202 $compiler->template->variable_filters = $_tpl->variable_filters;
203 if ($_tpl->has_nocache_code) {
204 $compiler->template->has_nocache_code = true;
206 foreach ($_tpl->required_plugins as $key => $tmp1) {
207 if ($compiler->nocache && $compiler->template->caching) {
212 foreach ($tmp1 as $name => $tmp) {
213 foreach ($tmp as $type => $data) {
214 $compiler->template->required_plugins[$code][$name][$type] = $data;
225 * Smarty Internal Plugin Compile BlockClose Class
228 * @subpackage Compiler
230 class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
233 * Compiles code for the {/block} tag
235 * @param array $args array with attributes from parser
236 * @param object $compiler compiler object
237 * @return string compiled code
239 public function compile($args, $compiler) {
240 $compiler->has_code = true;
241 // check and get attributes
242 $_attr = $this->getAttributes($compiler, $args);
243 $saved_data = $this->closeTag($compiler, array('block'));
244 $_name = trim($saved_data[0]['name'], "\"'");
245 if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
246 // restore to status before {block} tag as new subtemplate code of parent {block} is not needed
247 // TODO: Below code was disabled in 3.1.8 because of problems with {include} in nested {block} tags in child templates
248 // combined with append/prepend or $smarty.block.parent
249 // For later versions it should be checked under which conditions it could run for optimisation
251 //$compiler->merged_templates = $saved_data[4];
252 //$compiler->smarty->merged_templates_func = $saved_data[5];
253 //$compiler->template->properties = $saved_data[6];
254 //$compiler->template->has_nocache_code = $saved_data[7];
255 $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
257 if (isset($saved_data[0]['hide']) && !isset($compiler->template->block_data[$_name]['source'])) {
260 $_output = $compiler->parser->current_buffer->to_smarty_php();
262 unset($compiler->template->block_data[$_name]['compiled']);
265 $compiler->parser->current_buffer = $saved_data[1];
266 $compiler->nocache = $saved_data[2];
267 $compiler->smarty->merge_compiled_includes = $saved_data[3];
268 // reset flag for {block} tag
269 $compiler->inheritance = false;
270 // $_output content has already nocache code processed
271 $compiler->suppressNocacheProcessing = true;