3 * Smarty Internal Plugin Compile Include
4 * Compiles the {include} tag
12 * Smarty Internal Plugin Compile Include Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
20 * caching mode to create nocache code but no cache file
22 const CACHING_NOCACHE_CODE = 9999;
24 * Attribute definition: Overwrites base class.
27 * @see Smarty_Internal_CompileBase
29 public $required_attributes = array('file');
31 * Attribute definition: Overwrites base class.
34 * @see Smarty_Internal_CompileBase
36 public $shorttag_order = array('file');
38 * Attribute definition: Overwrites base class.
41 * @see Smarty_Internal_CompileBase
43 public $option_flags = array('nocache', 'inline', 'caching');
45 * Attribute definition: Overwrites base class.
48 * @see Smarty_Internal_CompileBase
50 public $optional_attributes = array('_any');
53 * Compiles code for the {include} tag
55 * @param array $args array with attributes from parser
56 * @param object $compiler compiler object
57 * @param array $parameter array with compilation parameter
59 * @return string compiled code
61 public function compile($args, $compiler, $parameter)
63 // check and get attributes
64 $_attr = $this->getAttributes($compiler, $args);
65 // save possible attributes
66 $include_file = $_attr['file'];
68 if (isset($_attr['assign'])) {
69 // output will be stored in a smarty variable instead of being displayed
70 $_assign = $_attr['assign'];
73 $_parent_scope = Smarty::SCOPE_LOCAL;
74 if (isset($_attr['scope'])) {
75 $_attr['scope'] = trim($_attr['scope'], "'\"");
76 if ($_attr['scope'] == 'parent') {
77 $_parent_scope = Smarty::SCOPE_PARENT;
78 } elseif ($_attr['scope'] == 'root') {
79 $_parent_scope = Smarty::SCOPE_ROOT;
80 } elseif ($_attr['scope'] == 'global') {
81 $_parent_scope = Smarty::SCOPE_GLOBAL;
85 $_caching = Smarty::CACHING_OFF;
87 // flag if included template code should be merged into caller
88 $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) || $_attr['inline'] === true) && !$compiler->template->source->recompiled;
90 // set default when in nocache mode
91 // if ($compiler->template->caching && ($compiler->nocache || $compiler->tag_nocache || $compiler->forceNocache == 2)) {
92 if ($compiler->template->caching && ((!$compiler->inheritance && !$compiler->nocache && !$compiler->tag_nocache) || ($compiler->inheritance && ($compiler->nocache || $compiler->tag_nocache)))) {
93 $_caching = self::CACHING_NOCACHE_CODE;
96 * if the {include} tag provides individual parameter for caching
97 * it will not be included into the common cache file and treated like
100 if (isset($_attr['cache_lifetime'])) {
101 $_cache_lifetime = $_attr['cache_lifetime'];
102 $compiler->tag_nocache = true;
103 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
105 $_cache_lifetime = 'null';
107 if (isset($_attr['cache_id'])) {
108 $_cache_id = $_attr['cache_id'];
109 $compiler->tag_nocache = true;
110 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
112 $_cache_id = '$_smarty_tpl->cache_id';
114 if (isset($_attr['compile_id'])) {
115 $_compile_id = $_attr['compile_id'];
117 $_compile_id = '$_smarty_tpl->compile_id';
119 if ($_attr['caching'] === true) {
120 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
122 if ($_attr['nocache'] === true) {
123 $compiler->tag_nocache = true;
124 if ($merge_compiled_includes) {
125 $_caching = self::CACHING_NOCACHE_CODE;
127 $_caching = Smarty::CACHING_OFF;
131 $has_compiled_template = false;
132 if ($merge_compiled_includes && $_attr['inline'] !== true) {
133 // variable template name ?
134 if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2))
135 || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
137 $merge_compiled_includes = false;
138 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
139 $compiler->trigger_template_error(' variable template file names not allow within {block} tags');
142 // variable compile_id?
143 if (isset($_attr['compile_id'])) {
144 if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2))
145 || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
147 $merge_compiled_includes = false;
148 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
149 $compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
154 if ($merge_compiled_includes) {
155 if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
156 $merge_compiled_includes = false;
157 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
158 $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
162 if ($merge_compiled_includes) {
163 // we must observe different compile_id
164 $uid = sha1($_compile_id);
167 /** @var Smarty_Internal_Template $_smarty_tpl
168 * used in evaluated code
170 $_smarty_tpl = $compiler->template;
171 eval("\$tpl_name = $include_file;");
172 if (!isset($compiler->smarty->merged_templates_func[$tpl_name][$uid])) {
173 $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
174 // save unique function name
175 $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
176 // use current nocache hash for inlined code
177 $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
178 if ($compiler->template->caching && $_caching == self::CACHING_NOCACHE_CODE) {
179 // all code must be nocache
182 if ($compiler->inheritance) {
183 $tpl->compiler->inheritance = true;
185 // make sure whole chain gets compiled
186 $tpl->mustCompile = true;
187 if (!($tpl->source->uncompiled) && $tpl->source->exists) {
190 $compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
191 // release compiler object to free memory
192 unset($tpl->compiler);
193 // merge compiled code for {function} tags
194 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
195 // merge filedependency
196 $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp, $tpl->source->type);
197 $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
198 // remove header code
199 $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
200 if ($tpl->has_nocache_code) {
201 // replace nocache_hash
202 $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
203 $compiler->template->has_nocache_code = true;
205 $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
206 $has_compiled_template = true;
210 $has_compiled_template = true;
213 // delete {include} standard attributes
214 unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
215 // remaining attributes must be assigned as smarty variable
216 if (!empty($_attr)) {
217 if ($_parent_scope == Smarty::SCOPE_LOCAL) {
220 foreach ($_attr as $key => $value) {
221 $_pairs[] = "'$key'=>$value";
222 $nccode .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_variable($value);\n";
224 $_vars = 'array(' . join(',', $_pairs) . ')';
226 $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
231 if ($has_compiled_template) {
232 // never call inline templates in nocache mode
233 $compiler->suppressNocacheProcessing = true;
234 $_hash = $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'];
235 $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
236 $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
237 if (!empty($nccode) && $_caching == 9999 && $_smarty_tpl->caching) {
238 $compiler->suppressNocacheProcessing = false;
239 $_output .= substr($compiler->processNocacheCode('<?php ' .$nccode . "?>\n", true), 6, -3);
240 $compiler->suppressNocacheProcessing = true;
242 $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
243 if (isset($_assign)) {
244 $_output .= 'ob_start(); ';
246 $_output .= $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] . "(\$_smarty_tpl);\n";
247 $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
248 if (isset($_assign)) {
249 $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
251 $_output .= "\n/* End of included template \"" . $tpl_name . "\" */?>";
256 // was there an assign attribute
257 if (isset($_assign)) {
258 $_output = "<?php \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope));?>\n";;
260 $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";