1<?php 2/** 3 * Smarty Internal Extension 4 * This file contains the Smarty template extension to create a code frame 5 * 6 * @package Smarty 7 * @subpackage Template 8 * @author Uwe Tews 9 */ 10 11/** 12 * Class Smarty_Internal_Extension_CodeFrame 13 * Create code frame for compiled and cached templates 14 */ 15class Smarty_Internal_Runtime_CodeFrame 16{ 17 /** 18 * Create code frame for compiled and cached templates 19 * 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 25 * 26 * @return string 27 */ 28 public function create( 29 Smarty_Internal_Template $_template, 30 $content = '', 31 $functions = '', 32 $cache = false, 33 Smarty_Internal_TemplateCompilerBase $compiler = null 34 ) { 35 // build property code 36 $properties[ 'version' ] = Smarty::SMARTY_VERSION; 37 $properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); 38 if (!$cache) { 39 $properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code; 40 $properties[ 'file_dependency' ] = $_template->compiled->file_dependency; 41 $properties[ 'includes' ] = $_template->compiled->includes; 42 } else { 43 $properties[ 'has_nocache_code' ] = $_template->cached->has_nocache_code; 44 $properties[ 'file_dependency' ] = $_template->cached->file_dependency; 45 $properties[ 'cache_lifetime' ] = $_template->cache_lifetime; 46 } 47 $output = "<?php\n"; 48 $output .= "/* Smarty version {$properties[ 'version' ]}, created on " . strftime("%Y-%m-%d %H:%M:%S") . 49 "\n from '" . str_replace('*/', '* /', $_template->source->filepath) . "' */\n\n"; 50 $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n"; 51 $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' . 52 ($cache ? 'true' : 'false') . ')'; 53 $output .= "if ({$dec}) {\n"; 54 $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n"; 55 if (!$cache && !empty($compiler->tpl_function)) { 56 $output .= '$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions($_smarty_tpl, '; 57 $output .= var_export($compiler->tpl_function, true); 58 $output .= ");\n"; 59 } 60 if ($cache && isset($_template->smarty->ext->_tplFunction)) { 61 $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " . 62 var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n"; 63 } 64 $output .= "?>"; 65 $output .= $content; 66 $output .= "<?php }\n?>"; 67 $output .= $functions; 68 $output .= "<?php }\n"; 69 // remove unneeded PHP tags 70 if (preg_match('/\s*\?>[\n]?<\?php\s*/', $output)) { 71 $curr_split = preg_split( 72 '/\s*\?>[\n]?<\?php\s*/', 73 $output 74 ); 75 preg_match_all( 76 '/\s*\?>[\n]?<\?php\s*/', 77 $output, 78 $curr_parts 79 ); 80 $output = ''; 81 foreach ($curr_split as $idx => $curr_output) { 82 $output .= $curr_output; 83 if (isset($curr_parts[ 0 ][ $idx ])) { 84 $output .= "\n"; 85 } 86 } 87 } 88 if (preg_match('/\?>\s*$/', $output)) { 89 $curr_split = preg_split( 90 '/\?>\s*$/', 91 $output 92 ); 93 $output = ''; 94 foreach ($curr_split as $idx => $curr_output) { 95 $output .= $curr_output; 96 } 97 } 98 return $output; 99 } 100} 101