4 * Smarty Internal Plugin Compile Include
6 * Compiles the {include} tag
14 * Smarty Internal Plugin Compile Include Class
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');
26 * Compiles code for the {include} tag
28 * @param array $args array with attributes from parser
29 * @param object $compiler compiler object
30 * @return string compiled code
32 public function compile($args, $compiler)
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) {
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;
53 // if ($this->compiler->template->mustCompile) {
54 // make sure whole chain gest compiled
55 $tpl->mustCompile = true;
57 if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
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;
68 $compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
69 $compiler->template->has_nocache_code = $saved_has_nocache_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;
77 $has_compiled_template = true;
83 if (isset($_attr['assign'])) {
84 // output will be stored in a smarty variable instead of beind displayed
85 $_assign = $_attr['assign'];
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;
100 if ($this->compiler->nocache || $this->compiler->tag_nocache) {
101 $_caching = Smarty::CACHING_OFF;
103 // default for included templates
104 if ($this->compiler->template->caching && !$this->compiler->nocache && !$this->compiler->tag_nocache) {
105 $_caching = self::CACHING_NOCACHE_CODE;
108 * if the {include} tag provides individual parameter for caching
109 * it will not be included into the common cache file and treated like
112 if (isset($_attr['cache_lifetime'])) {
113 $_cache_lifetime = $_attr['cache_lifetime'];
114 $this->compiler->tag_nocache = true;
115 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
117 $_cache_lifetime = 'null';
119 if (isset($_attr['cache_id'])) {
120 $_cache_id = $_attr['cache_id'];
121 $this->compiler->tag_nocache = true;
122 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
124 $_cache_id = '$_smarty_tpl->cache_id';
126 if (isset($_attr['compile_id'])) {
127 $_compile_id = $_attr['compile_id'];
129 $_compile_id = '$_smarty_tpl->compile_id';
131 if ($_attr['caching'] === true) {
132 $_caching = Smarty::CACHING_LIFETIME_CURRENT;
134 if ($_attr['nocache'] === true) {
135 $this->compiler->tag_nocache = true;
136 $_caching = Smarty::CACHING_OFF;
138 // create template object
139 $_output = "<?php \$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, $_cache_id, $_compile_id, $_caching, $_cache_lifetime);\n";
140 // delete {include} standard attributes
141 unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
142 // remaining attributes must be assigned as smarty variable
143 if (!empty($_attr)) {
144 if ($_parent_scope == Smarty::SCOPE_LOCAL) {
146 foreach ($_attr as $_key => $_value) {
147 $_output .= "\$_template->assign('$_key',$_value);";
150 $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope', $this->compiler->lex->taglineno);
153 // was there an assign attribute
154 if (isset($_assign)) {
155 $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
157 if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
158 $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
159 $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
160 $_output .= $compiled_tpl;
161 $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
162 $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
163 $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
165 $_output .= " echo \$_template->getRenderedTemplate();?>";
166 if ($_parent_scope != Smarty::SCOPE_LOCAL) {
167 $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
171 $_output .= "<?php unset(\$_template);?>";