3 * Smarty Internal Plugin Compile Include
5 * Compiles the {include} tag
13 * Smarty Internal Plugin Compile Include Class
16 * @subpackage Compiler
18 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
21 * caching mode to create nocache code but no cache file
23 const CACHING_NOCACHE_CODE = 9999;
25 * Attribute definition: Overwrites base class.
28 * @see Smarty_Internal_CompileBase
30 public $required_attributes = array('file');
32 * Attribute definition: Overwrites base class.
35 * @see Smarty_Internal_CompileBase
37 public $shorttag_order = array('file');
39 * Attribute definition: Overwrites base class.
42 * @see Smarty_Internal_CompileBase
44 public $option_flags = array('nocache', 'inline', 'caching');
46 * Attribute definition: Overwrites base class.
49 * @see Smarty_Internal_CompileBase
51 public $optional_attributes = array('_any');
54 * Compiles code for the {include} tag
56 * @param array $args array with attributes from parser
57 * @param object $compiler compiler object
58 * @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 posible attributes
66 $include_file = $_attr['file'];
68 if (isset($_attr['assign'])) {
69 // output will be stored in a smarty variable instead of beind 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 if ($compiler->nocache || $compiler->tag_nocache) {
86 $_caching = Smarty::CACHING_OFF;
88 // default for included templates
89 if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
90 $_caching = self::CACHING_NOCACHE_CODE;
93 * if the {include} tag provides individual parameter for caching
94 * it will not be included into the common cache file and treated like
97 if (isset($_attr['cache_lifetime'])) {
98 $_cache_lifetime = $_attr['cache_lifetime'];
99 $compiler->tag_nocache = true;
100 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
102 $_cache_lifetime = 'null';
104 if (isset($_attr['cache_id'])) {
105 $_cache_id = $_attr['cache_id'];
106 $compiler->tag_nocache = true;
107 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
109 $_cache_id = '$_smarty_tpl->cache_id';
111 if (isset($_attr['compile_id'])) {
112 $_compile_id = $_attr['compile_id'];
114 $_compile_id = '$_smarty_tpl->compile_id';
116 if ($_attr['caching'] === true) {
117 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
119 if ($_attr['nocache'] === true) {
120 $compiler->tag_nocache = true;
121 $_caching = Smarty::CACHING_OFF;
124 $has_compiled_template = false;
125 if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled
126 && !($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache)) && $_caching != Smarty::CACHING_LIFETIME_CURRENT) {
127 // check if compiled code can be merged (contains no variable part)
128 if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2)
129 and substr_count($include_file, '(') == 0 and substr_count($include_file, '$_smarty_tpl->') == 0) {
131 eval("\$tpl_name = $include_file;");
132 if (!isset($compiler->smarty->merged_templates_func[$tpl_name]) || $compiler->inheritance) {
133 $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
134 // save unique function name
135 $compiler->smarty->merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'. str_replace('.', '_', uniqid('', true));
136 // use current nocache hash for inlined code
137 $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
138 if ($compiler->template->caching) {
139 // needs code for cached page but no cache file
140 $tpl->caching = self::CACHING_NOCACHE_CODE;
142 // make sure whole chain gest compiled
143 $tpl->mustCompile = true;
144 if (!($tpl->source->uncompiled) && $tpl->source->exists) {
146 $compiled_code = $tpl->compiler->compileTemplate($tpl);
147 // release compiler object to free memory
148 unset($tpl->compiler);
149 // merge compiled code for {function} tags
150 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
151 // merge filedependency
152 $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp,$tpl->source->type);
153 $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
154 // remove header code
155 $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
156 if ($tpl->has_nocache_code) {
157 // replace nocache_hash
158 $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
159 $compiler->template->has_nocache_code = true;
161 $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
162 $has_compiled_template = true;
165 $has_compiled_template = true;
169 // delete {include} standard attributes
170 unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
171 // remaining attributes must be assigned as smarty variable
172 if (!empty($_attr)) {
173 if ($_parent_scope == Smarty::SCOPE_LOCAL) {
175 foreach ($_attr as $key => $value) {
176 $_pairs[] = "'$key'=>$value";
178 $_vars = 'array('.join(',',$_pairs).')';
181 $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
187 if ($has_compiled_template) {
188 $_hash = $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'];
189 $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
190 $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
191 $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
192 if (isset($_assign)) {
193 $_output .= 'ob_start(); ';
195 $_output .= $compiler->smarty->merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n";
196 $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
197 if (isset($_assign)) {
198 $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
200 $_output .= "/* End of included template \"" . $tpl_name . "\" */?>";
204 // was there an assign attribute
205 if (isset($_assign)) {
206 $_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";;
208 $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";