a3b3c9444a3da22ca45003e37183ccaa8d531a1a
1 <?php
3 /**
4 * Smarty Internal Plugin Compile Include
5 *
6 * Compiles the {include} tag
7 *
8 * @package Smarty
9 * @subpackage Compiler
10 * @author Uwe Tews
11 */
13 /**
14 * Smarty Internal Plugin Compile Include Class
15 */
16 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
17 // caching mode to create nocache code but no cache file
18 const CACHING_NOCACHE_CODE = 9999;
19 // attribute definitions
20 public $required_attributes = array('file');
21 public $shorttag_order = array('file');
22 public $option_flags = array('nocache','inline','caching');
23 public $optional_attributes = array('_any');
25 /**
26 * Compiles code for the {include} tag
27 *
28 * @param array $args array with attributes from parser
29 * @param object $compiler compiler object
30 * @return string compiled code
31 */
32 public function compile($args, $compiler)
33 {
34 $this->compiler = $compiler;
35 // check and get attributes
36 $_attr = $this->_get_attributes($args);
37 // save posible attributes
38 $include_file = $_attr['file'];
39 $has_compiled_template = false;
40 if ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) {
41 // check if compiled code can be merged (contains no variable part)
42 if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
43 $tmp = null;
44 eval("\$tmp = $include_file;");
45 if ($this->compiler->template->template_resource != $tmp) {
46 $tpl = new $compiler->smarty->template_class ($tmp, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
47 // suppress writing of compiled file
48 $tpl->write_compiled_code = false;
49 if ($this->compiler->template->caching) {
50 // needs code for cached page but no cache file
51 $tpl->caching = self::CACHING_NOCACHE_CODE;
52 }
53 // if ($this->compiler->template->mustCompile) {
54 // make sure whole chain gest compiled
55 $tpl->mustCompile = true;
56 // }
57 if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
58 // get compiled code
59 $compiled_tpl = $tpl->getCompiledTemplate();
60 // merge compiled code for {function} tags
61 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
62 // merge filedependency by evaluating header code
63 preg_match_all("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", $compiled_tpl, $result);
64 $saved_has_nocache_code = $compiler->template->has_nocache_code;
65 $saved_nocache_hash = $compiler->template->properties['nocache_hash'];
66 $_smarty_tpl = $compiler->template;
67 eval($result[2][0]);
68 $compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
69 $compiler->template->has_nocache_code = $saved_has_nocache_code;
70 // remove header code
71 $compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
72 if ($tpl->has_nocache_code) {
73 // replace nocache_hash
74 $compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
75 $compiler->template->has_nocache_code = true;
76 }
77 $has_compiled_template = true;
78 }
79 }
80 }
81 }
83 if (isset($_attr['assign'])) {
84 // output will be stored in a smarty variable instead of beind displayed
85 $_assign = $_attr['assign'];
86 }
88 $_parent_scope = Smarty::SCOPE_LOCAL;
89 if (isset($_attr['scope'])) {
90 $_attr['scope'] = trim($_attr['scope'], "'\"");
91 if ($_attr['scope'] == 'parent') {
92 $_parent_scope = Smarty::SCOPE_PARENT;
93 } elseif ($_attr['scope'] == 'root') {
94 $_parent_scope = Smarty::SCOPE_ROOT;
95 } elseif ($_attr['scope'] == 'global') {
96 $_parent_scope = Smarty::SCOPE_GLOBAL;
97 }
98 }
99 $_caching = 'null';
100 if ($this->compiler->nocache || $this->compiler->tag_nocache) {
101 $_caching = Smarty::CACHING_OFF;
102 }
103 // default for included templates
104 if ($compiler->template->caching && !$this->compiler->nocache && !$this->compiler->tag_nocache) {
105 $_caching = self::CACHING_NOCACHE_CODE;
106 }
107 /*
108 * if the {include} tag provides individual parameter for caching
109 * it will not be included into the common cache file and treated like
110 * a nocache section
111 */
112 if (isset($_attr['cache_lifetime'])) {
113 $_cache_lifetime = $_attr['cache_lifetime'];
114 $this->compiler->tag_nocache = true;
115 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
116 } else {
117 $_cache_lifetime = 'null';
118 }
119 if (isset($_attr['cache_id'])) {
120 $_cache_id = $_attr['cache_id'];
121 $this->compiler->tag_nocache = true;
122 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
123 } else {
124 $_cache_id = '$_smarty_tpl->cache_id';
125 }
126 if (isset($_attr['compile_id'])) {
127 $_compile_id = $_attr['compile_id'];
128 } else {
129 $_compile_id = '$_smarty_tpl->compile_id';
130 }
131 if ($_attr['caching'] === true) {
132 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
133 }
134 if ($_attr['nocache'] === true) {
135 $this->compiler->tag_nocache = true;
136 $_caching = Smarty::CACHING_OFF;
137 }
138 // create template object
139 $_output = "<?php ";
140 if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) {
141 $_output .= "\$sha = sha1($include_file . $_cache_id . $_compile_id);\n";
142 $_output .= "if (isset(\$_smarty_tpl->smarty->template_objects[\$sha])) {\n";
143 $_output .= "\$_template = \$_smarty_tpl->smarty->template_objects[\$sha]; \$_template->caching = $_caching; \$_template->cache_lifetime = $_cache_lifetime;\n";
144 $_output .= "} else {\n";
145 }
146 $_output .= "\$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, $_cache_id, $_compile_id, $_caching, $_cache_lifetime);\n";
147 if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) {
148 $_output .= "}\n";
149 }
150 // delete {include} standard attributes
151 unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
152 // remaining attributes must be assigned as smarty variable
153 if (!empty($_attr)) {
154 if ($_parent_scope == Smarty::SCOPE_LOCAL) {
155 // create variables
156 foreach ($_attr as $_key => $_value) {
157 $_output .= "\$_template->assign('$_key',$_value);";
158 }
159 } else {
160 $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope', $this->compiler->lex->taglineno);
161 }
162 }
163 // was there an assign attribute
164 if (isset($_assign)) {
165 $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
166 } else {
167 if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
168 $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
169 $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
170 $_output .= $compiled_tpl;
171 $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
172 $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
173 $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
174 } else {
175 $_output .= " echo \$_template->getRenderedTemplate(); \$_template->rendered_content = null;?>";
176 if ($_parent_scope != Smarty::SCOPE_LOCAL) {
177 $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
178 }
179 }
180 }
181 $_output .= "<?php unset(\$_template);?>";
182 return $_output;
183 }
184 }
186 ?>