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_Extension_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 bool $cache flag for cache file
26 public static function create(Smarty_Internal_Template
$_template, $content = '', $cache = false)
28 // build property code
29 $_template->properties
['has_nocache_code'] = $_template->has_nocache_code ||
!empty($_template->required_plugins
['nocache']);
30 $_template->properties
['version'] = Smarty
::SMARTY_VERSION
;
31 if (!isset($_template->properties
['unifunc'])) {
32 $_template->properties
['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
34 $properties = $_template->properties
;
36 unset($properties['tpl_function']);
37 if (!empty($_template->compiler
->templateProperties
)) {
38 $properties['tpl_function'] = $_template->compiler
->templateProperties
['tpl_function'];
42 $output .= "/*%%SmartyHeaderCode:{$_template->properties['nocache_hash']}%%*/\n";
43 if ($_template->smarty
->direct_access_security
) {
44 $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
46 $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($properties, true) . ',' . ($cache ?
'true' : 'false') . ");\n";
47 $output .= "/*/%%SmartyHeaderCode%%*/\n";
48 $output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {\n";
49 $output .= "function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n";
50 // include code for plugins
52 if (!empty($_template->required_plugins
['compiled'])) {
53 foreach ($_template->required_plugins
['compiled'] as $tmp) {
54 foreach ($tmp as $data) {
55 $file = addslashes($data['file']);
56 if (is_Array($data['function'])) {
57 $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n";
59 $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n";
64 if (!empty($_template->required_plugins
['nocache'])) {
65 $_template->has_nocache_code
= true;
66 $output .= "echo '/*%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
67 foreach ($_template->required_plugins
['nocache'] as $tmp) {
68 foreach ($tmp as $data) {
69 $file = addslashes($data['file']);
70 if (is_Array($data['function'])) {
71 $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n");
73 $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n");
77 $output .= "?>/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/';\n";
81 $output = self
::appendCode($output, $content);
82 return self
::appendCode($output, "<?php }\n}\n?>");
86 * Create code frame of compiled template function
88 * @param \Smarty_Internal_Template $_template
89 * @param string $content
93 public static function createFunctionFrame(Smarty_Internal_Template
$_template, $content = '')
95 if (!isset($_template->properties
['unifunc'])) {
96 $_template->properties
['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
99 $output .= "/*%%SmartyHeaderCode:{$_template->properties['nocache_hash']}%%*/\n";
100 $output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {\n";
101 $output .= "function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n";
102 $output .= "?>\n" . $content;
103 $output .= "<?php\n";
104 $output .= "/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/\n";
105 $output .= "}\n}\n?>";
110 * Append code segments and remove unneeded ?> <?php transitions
112 * @param string $left
113 * @param string $right
117 public static function appendCode($left, $right)
119 if (preg_match('/\s*\?>$/', $left) && preg_match('/^<\?php\s+/', $right)) {
120 $left = preg_replace('/\s*\?>$/', "\n", $left);
121 $left .= preg_replace('/^<\?php\s+/', '', $right);