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 */
12 /**
13 * Smarty Internal Plugin Compile Include Class
14 */
15 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
16 /**
17 * Compiles code for the {include} tag
18 *
19 * @param array $args array with attributes from parser
20 * @param object $compiler compiler object
21 * @return string compiled code
22 */
23 public function compile($args, $compiler)
24 {
25 $this->compiler = $compiler;
26 $this->required_attributes = array('file');
27 $this->optional_attributes = array('_any');
28 // check and get attributes
29 $_attr = $this->_get_attributes($args);
30 // save posible attributes
31 $include_file = $_attr['file'];
32 $has_compiled_template = false;
33 if ($compiler->smarty->merge_compiled_includes || isset($_attr['inline'])) {
34 // check if compiled code can be merged (contains no variable part)
35 if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
36 eval("\$tmp = $include_file;");
37 if ($this->compiler->template->template_resource != $tmp) {
38 $tpl = $compiler->smarty->createTemplate ($tmp, $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
39 if ($this->compiler->template->caching) {
40 // needs code for cached page but no cache file
41 $tpl->caching = 9999;
42 }
43 if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
44 // make sure that template is up to date and merge template properties
45 $tpl->renderTemplate();
46 // compiled code for {function} tags
47 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
48 // get compiled code
49 $compiled_tpl = $tpl->getCompiledTemplate();
50 // remove header code
51 $compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
52 if ($tpl->has_nocache_code) {
53 // replace nocache_hash
54 $compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
55 $compiler->template->has_nocache_code = true;
56 }
57 $has_compiled_template = true;
58 }
59 }
60 }
61 }
63 if (isset($_attr['assign'])) {
64 // output will be stored in a smarty variable instead of beind displayed
65 $_assign = $_attr['assign'];
66 }
68 $_parent_scope = SMARTY_LOCAL_SCOPE;
69 if (isset($_attr['scope'])) {
70 if ($_attr['scope'] == '\'parent\'') {
71 $_parent_scope = SMARTY_PARENT_SCOPE;
72 } elseif ($_attr['scope'] == '\'root\'') {
73 $_parent_scope = SMARTY_ROOT_SCOPE;
74 } elseif ($_attr['scope'] == '\'global\'') {
75 $_parent_scope = SMARTY_GLOBAL_SCOPE;
76 }
77 }
78 $_caching = 'null';
79 // default for included templates
80 if ($this->compiler->template->caching && !$this->compiler->nocache) {
81 $_caching = 9999;
82 }
83 /*
84 * if the {include} tag provides individual parameter for caching
85 * it will not be included into the common cache file and treated like
86 * a nocache section
87 */
88 if (isset($_attr['cache_lifetime'])) {
89 $_cache_lifetime = $_attr['cache_lifetime'];
90 $this->compiler->tag_nocache = true;
91 $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
92 } else {
93 $_cache_lifetime = 'null';
94 }
95 if (isset($_attr['nocache'])) {
96 if ($_attr['nocache'] == 'true') {
97 $this->compiler->tag_nocache = true;
98 $_caching = SMARTY_CACHING_OFF;
99 }
100 }
101 if (isset($_attr['caching'])) {
102 if ($_attr['caching'] == 'true') {
103 $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
104 } else {
105 $this->compiler->tag_nocache = true;
106 $_caching = SMARTY_CACHING_OFF;
107 }
108 }
109 // create template object
110 $_output = "<?php \$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id, $_caching, $_cache_lifetime);\n";
111 // delete {include} standard attributes
112 unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
113 // remaining attributes must be assigned as smarty variable
114 if (!empty($_attr)) {
115 if ($_parent_scope == SMARTY_LOCAL_SCOPE) {
116 // create variables
117 foreach ($_attr as $_key => $_value) {
118 $_output .= "\$_template->assign('$_key',$_value);";
119 }
120 } else {
121 $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope');
122 }
123 }
124 // was there an assign attribute
125 if (isset($_assign)) {
126 $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
127 } else {
128 if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
129 $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
130 $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
131 $_output .= $compiled_tpl;
132 $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
133 $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
134 $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
135 } else {
136 $_output .= " echo \$_template->getRenderedTemplate();?>";
137 $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
138 }
139 }
140 $_output .= "<?php unset(\$_template);?>\n";
141 return $_output;
142 }
143 }
145 ?>