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