К новейшей редакции | Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
304 | alex-w | 1 | <?php |
2 | |||
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 (true) { |
||
34 | // check if compiled code can be merged |
||
35 | if (strpos($include_file, '$_smarty_tpl') === false) { |
||
36 | $tpl = $compiler->smarty->createTemplate (trim($include_file, "'\""), $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template); |
||
37 | do { |
||
38 | $must_compile = false; |
||
39 | $prop = array(); |
||
40 | $compiled_tpl = $tpl->getCompiledTemplate(); |
||
41 | preg_match('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>)/', $compiled_tpl, $matches); |
||
42 | $compiled_tpl = preg_replace(array('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>.*\n)/','/(\<\?php if\(\!defined\(\'SMARTY_DIR\'\)\))(.*)(\?\>.*\n)/'), '', $compiled_tpl); |
||
43 | //var_dump($matches, $compiled_tpl); |
||
44 | if (isset($matches[2])) { |
||
45 | $prop = unserialize($matches[2]); |
||
46 | foreach ($prop['file_dependency'] as $_file_to_check) { |
||
47 | If (is_file($_file_to_check[0])) { |
||
48 | $mtime = filemtime($_file_to_check[0]); |
||
49 | } else { |
||
50 | $tpl->parseResourceName($_file_to_check[0], $resource_type, $resource_name, $resource_handler); |
||
51 | $mtime = $resource_handler->getTemplateTimestampTypeName($resource_type, $resource_name); |
||
52 | } |
||
53 | If ($mtime != $_file_to_check[1]) { |
||
54 | $must_compile = true; |
||
55 | break; |
||
56 | } |
||
57 | } |
||
58 | if ($must_compile) { |
||
59 | // recompile |
||
60 | $tpl->compileTemplateSource(); |
||
61 | } |
||
62 | } |
||
63 | } while ($must_compile); |
||
64 | if (isset($prop['file_dependency'])) { |
||
65 | $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $prop['file_dependency']); |
||
66 | } |
||
67 | $has_compiled_template = true; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if (isset($_attr['assign'])) { |
||
72 | // output will be stored in a smarty variable instead of beind displayed |
||
73 | $_assign = $_attr['assign']; |
||
74 | } |
||
75 | |||
76 | $_parent_scope = SMARTY_LOCAL_SCOPE; |
||
77 | if (isset($_attr['scope'])) { |
||
78 | if ($_attr['scope'] == '\'parent\'') { |
||
79 | $_parent_scope = SMARTY_PARENT_SCOPE; |
||
80 | } elseif ($_attr['scope'] == '\'root\'') { |
||
81 | $_parent_scope = SMARTY_ROOT_SCOPE; |
||
82 | } elseif ($_attr['scope'] == '\'global\'') { |
||
83 | $_parent_scope = SMARTY_GLOBAL_SCOPE; |
||
84 | } |
||
85 | } |
||
86 | // default for included templates |
||
87 | if ($compiler->template->caching) { |
||
88 | $_caching = SMARTY_CACHING_LIFETIME_CURRENT; |
||
89 | } else { |
||
90 | $_caching = SMARTY_CACHING_OFF; |
||
91 | } |
||
92 | /* |
||
93 | * if the {include} tag provides individual parameter for caching |
||
94 | * it will not be included into the common cache file and treated like |
||
95 | * a nocache section |
||
96 | */ |
||
97 | if (isset($_attr['cache_lifetime'])) { |
||
98 | $_cache_lifetime = $_attr['cache_lifetime']; |
||
99 | $this->compiler->tag_nocache = true; |
||
100 | } |
||
101 | if (isset($_attr['nocache'])) { |
||
102 | if ($_attr['nocache'] == 'true') { |
||
103 | $this->compiler->tag_nocache = true; |
||
104 | } |
||
105 | } |
||
106 | if (isset($_attr['caching'])) { |
||
107 | if ($_attr['caching'] == 'true') { |
||
108 | $_caching = SMARTY_CACHING_LIFETIME_CURRENT; |
||
109 | } else { |
||
110 | $_caching = SMARTY_CACHING_OFF; |
||
111 | } |
||
112 | } |
||
113 | // create template object |
||
114 | $_output = "<?php \$_template = new Smarty_Template ($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id);"; |
||
115 | // delete {include} standard attributes |
||
116 | unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope']); |
||
117 | // remaining attributes must be assigned as smarty variable |
||
118 | if (!empty($_attr)) { |
||
119 | if ($_parent_scope == SMARTY_LOCAL_SCOPE) { |
||
120 | // create variables |
||
121 | foreach ($_attr as $_key => $_value) { |
||
122 | $_output .= "\$_template->assign('$_key',$_value);"; |
||
123 | } |
||
124 | } else { |
||
125 | $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope'); |
||
126 | } |
||
127 | } |
||
128 | // add caching parameter if required |
||
129 | if (isset($_cache_lifetime)) { |
||
130 | $_output .= "\$_template->cache_lifetime = $_cache_lifetime;"; |
||
131 | $_caching = SMARTY_CACHING_LIFETIME_CURRENT; |
||
132 | } |
||
133 | $_output .= "\$_template->caching = $_caching;"; |
||
134 | // was there an assign attribute |
||
135 | if (isset($_assign)) { |
||
136 | $_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>"; |
||
137 | } else { |
||
138 | if ($has_compiled_template) { |
||
139 | $_output .= " \$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n"; |
||
140 | $_output .= $compiled_tpl . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>";; |
||
141 | $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>"; |
||
142 | } else { |
||
143 | $_output .= " echo \$_smarty_tpl->smarty->fetch(\$_template); ?>"; |
||
144 | } |
||
145 | } |
||
146 | if ($_parent_scope != SMARTY_LOCAL_SCOPE) { |
||
147 | $_output .= "<?php \$_template->updateParentVariables($_parent_scope); ?>"; |
||
148 | } |
||
149 | return $_output; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | ?> |