3 * Smarty Internal Extension
4 * This file contains the Smarty template extension to create a code frame
12 * Class Smarty_Internal_Extension_CodeFrame
13 * Create code frame for compiled and cached templates
15 class Smarty_Internal_Runtime_CodeFrame
18 * Create code frame for compiled and cached templates
20 * @param Smarty_Internal_Template $_template
21 * @param string $content optional template content
22 * @param string $functions compiled template function and block code
23 * @param bool $cache flag for cache file
24 * @param \Smarty_Internal_TemplateCompilerBase $compiler
28 public function create(Smarty_Internal_Template $_template, $content = '', $functions = '', $cache = false,
29 Smarty_Internal_TemplateCompilerBase $compiler = null)
31 // build property code
32 $properties[ 'version' ] = Smarty::SMARTY_VERSION;
33 $properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
35 $properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code;
36 $properties[ 'file_dependency' ] = $_template->compiled->file_dependency;
37 $properties[ 'includes' ] = $_template->compiled->includes;
39 $properties[ 'has_nocache_code' ] = $_template->cached->has_nocache_code;
40 $properties[ 'file_dependency' ] = $_template->cached->file_dependency;
41 $properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
44 $output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
45 "\n from \"" . $_template->source->filepath . "\" */\n\n";
46 $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n";
47 $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
48 ($cache ? 'true' : 'false') . ")";
49 $output .= "if ({$dec}) {\n";
50 $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n";
51 if (!$cache && !empty($compiler->tpl_function)) {
52 $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
53 var_export($compiler->tpl_function, true) . ");\n";
55 if ($cache && isset($_template->smarty->ext->_tplFunction)) {
56 $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
57 var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n";
60 // include code for plugins
62 if (!empty($_template->compiled->required_plugins[ 'compiled' ])) {
63 foreach ($_template->compiled->required_plugins[ 'compiled' ] as $tmp) {
64 foreach ($tmp as $data) {
65 $file = addslashes($data[ 'file' ]);
66 if (is_array($data[ 'function' ])) {
67 $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n";
69 $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n";
74 if ($_template->caching && !empty($_template->compiled->required_plugins[ 'nocache' ])) {
75 $_template->compiled->has_nocache_code = true;
76 $output .= "echo '/*%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
77 foreach ($_template->compiled->required_plugins[ 'nocache' ] as $tmp) {
78 foreach ($tmp as $data) {
79 $file = addslashes($data[ 'file' ]);
80 if (is_array($data[ 'function' ])) {
81 $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n");
83 $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n");
87 $output .= "?>/*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/';\n";
92 $output .= "<?php }\n?>";
93 $output .= $functions;
94 $output .= "<?php }\n";
95 // remove unneeded PHP tags
96 return preg_replace(array('/\s*\?>[\n]?<\?php\s*/', '/\?>\s*$/'), array("\n", ''), $output);