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