|
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 // 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\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 $search = array("!({$_ldl}{$al}block[\s\S]*?{$name}[\s\S]*?{$_rdl})([\s\S]*?)({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})([\s\S]*?)({$_ldl}{$al}/block\s*{$_rdl})!", "/§§§child§§§/"); |
|
105 $replace = array('\2§§§child§§§\4', $replacement); |
|
106 $block_content = preg_replace($search, $replace, $block_content); |
|
107 } else { |
|
108 // remove hidden blocks |
|
109 $block_content = preg_replace("!({$_ldl}{$al}block[\s\S]*?{$name}[\s\S]*?{$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl})!", '', $block_content); |
|
110 } |
|
111 } |
|
112 } |
|
113 // do we have not nested {$smart.block.child} |
|
114 if (0 != preg_match("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_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]); |
|
119 } else { |
|
120 $replacement = ''; |
|
121 } |
|
122 $block_content = preg_replace("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $replacement, $block_content); |
|
123 } |
|
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']; |
|
132 } |
|
133 } else { |
|
134 $template->block_data[$_name]['source'] = $block_content; |
|
135 $template->block_data[$_name]['file'] = $filepath; |
|
136 } |
|
137 if ($_match[6] == 'append') { |
|
138 $template->block_data[$_name]['mode'] = 'append'; |
|
139 } elseif ($_match[6] == 'prepend') { |
|
140 $template->block_data[$_name]['mode'] = 'prepend'; |
|
141 } else { |
|
142 $template->block_data[$_name]['mode'] = 'replace'; |
|
143 } |
|
144 } |
|
145 } |
|
146 } |
|
147 |
|
148 /** |
|
149 * Compile saved child block source |
|
150 * |
|
151 * @param object $compiler compiler object |
|
152 * @param string $_name optional name of child block |
|
153 * @return string compiled code of schild block |
|
154 */ |
|
155 public static function compileChildBlock($compiler, $_name = null) { |
|
156 $_output = ''; |
|
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'], "'\""); |
|
163 break; |
|
164 } |
|
165 } |
|
166 // flag that child is already compile by {$smarty.block.child} inclusion |
|
167 $compiler->template->block_data[$_name]['compiled'] = true; |
|
168 } |
|
169 if ($_name == null) { |
|
170 $compiler->trigger_template_error('{$smarty.block.child} used out of context', $compiler->lex->taglineno); |
|
171 } |
|
172 // undefined child? |
|
173 if (!isset($compiler->template->block_data[$_name]['source'])) { |
|
174 return ''; |
|
175 } |
|
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; |
|
184 } else { |
|
185 $_tpl->compiler->forceNocache = 1; |
|
186 } |
|
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); |
|
198 } |
|
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; |
|
205 } |
|
206 foreach ($_tpl->required_plugins as $key => $tmp1) { |
|
207 if ($compiler->nocache && $compiler->template->caching) { |
|
208 $code = 'nocache'; |
|
209 } else { |
|
210 $code = $key; |
|
211 } |
|
212 foreach ($tmp1 as $name => $tmp) { |
|
213 foreach ($tmp as $type => $data) { |
|
214 $compiler->template->required_plugins[$code][$name][$type] = $data; |
|
215 } |
|
216 } |
|
217 } |
|
218 unset($_tpl); |
|
219 return $_output; |
|
220 } |
|
221 |
|
222 } |
|
223 |
|
224 /** |
|
225 * Smarty Internal Plugin Compile BlockClose Class |
|
226 * |
|
227 * @package Smarty |
|
228 * @subpackage Compiler |
|
229 */ |
|
230 class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase { |
|
231 |
|
232 /** |
|
233 * Compiles code for the {/block} tag |
|
234 * |
|
235 * @param array $args array with attributes from parser |
|
236 * @param object $compiler compiler object |
|
237 * @return string compiled code |
|
238 */ |
|
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 |
|
250 // |
|
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); |
|
256 } else { |
|
257 if (isset($saved_data[0]['hide']) && !isset($compiler->template->block_data[$_name]['source'])) { |
|
258 $_output = ''; |
|
259 } else { |
|
260 $_output = $compiler->parser->current_buffer->to_smarty_php(); |
|
261 } |
|
262 unset($compiler->template->block_data[$_name]['compiled']); |
|
263 } |
|
264 // reset flags |
|
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; |
|
272 return $_output; |
|
273 } |
|
274 |
|
275 } |
|
276 |
|
277 ?> |