mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-01 21:01:37 +01:00
- bugfix and enhancement
Because several recent problems with template inheritance the {block} tag compiler has been rewriten
- Error messages shown now the correct child template file and line number
- The compiler could fail on some larger UTF-8 text block (forum topic 24455}
- The {strip} tag can now be placed outside {block} tags in child templates (forum topic 24289}
- change SmartyException::$escape is now false by default
- change PHP traceback has been remove for SmartyException and SmartyCompilerException
This commit is contained in:
@@ -1,61 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty Internal Plugin Compile Include
|
||||
*
|
||||
* Compiles the {include} tag
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
* Smarty Internal Plugin Compile Include
|
||||
*
|
||||
* Compiles the {include} tag
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin Compile Include Class
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
*/
|
||||
* Smarty Internal Plugin Compile Include Class
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
*/
|
||||
class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
{
|
||||
/**
|
||||
* caching mode to create nocache code but no cache file
|
||||
*/
|
||||
* caching mode to create nocache code but no cache file
|
||||
*/
|
||||
const CACHING_NOCACHE_CODE = 9999;
|
||||
/**
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
public $required_attributes = array('file');
|
||||
/**
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
public $shorttag_order = array('file');
|
||||
/**
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
public $option_flags = array('nocache', 'inline', 'caching');
|
||||
/**
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
* Attribute definition: Overwrites base class.
|
||||
*
|
||||
* @var array
|
||||
* @see Smarty_Internal_CompileBase
|
||||
*/
|
||||
public $optional_attributes = array('_any');
|
||||
|
||||
/**
|
||||
* Compiles code for the {include} tag
|
||||
*
|
||||
* @param array $args array with attributes from parser
|
||||
* Compiles code for the {include} tag
|
||||
*
|
||||
* @param array $args array with attributes from parser
|
||||
* @param object $compiler compiler object
|
||||
* @param array $parameter array with compilation parameter
|
||||
* @param array $parameter array with compilation parameter
|
||||
* @return string compiled code
|
||||
*/
|
||||
public function compile($args, $compiler, $parameter)
|
||||
@@ -81,12 +81,15 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
$_parent_scope = Smarty::SCOPE_GLOBAL;
|
||||
}
|
||||
}
|
||||
$_caching = 'null';
|
||||
if ($compiler->nocache || $compiler->tag_nocache) {
|
||||
$_caching = Smarty::CACHING_OFF;
|
||||
}
|
||||
// default for included templates
|
||||
if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
|
||||
|
||||
$_caching = Smarty::CACHING_OFF;
|
||||
|
||||
// flag if included template code should be merged into caller
|
||||
$merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled;
|
||||
|
||||
// set default when in nocache mode
|
||||
// if ($compiler->template->caching && ($compiler->nocache || $compiler->tag_nocache || $compiler->forceNocache == 2)) {
|
||||
if ($compiler->template->caching && ((!$compiler->inheritance && !$compiler->nocache && !$compiler->tag_nocache) || ($compiler->inheritance && ($compiler->nocache ||$compiler->tag_nocache)))) {
|
||||
$_caching = self::CACHING_NOCACHE_CODE;
|
||||
}
|
||||
/*
|
||||
@@ -118,53 +121,89 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
}
|
||||
if ($_attr['nocache'] === true) {
|
||||
$compiler->tag_nocache = true;
|
||||
if ($merge_compiled_includes || $compiler->inheritance) {
|
||||
$_caching = self::CACHING_NOCACHE_CODE;
|
||||
} else {
|
||||
$_caching = Smarty::CACHING_OFF;
|
||||
}
|
||||
}
|
||||
|
||||
$has_compiled_template = false;
|
||||
if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled
|
||||
&& !($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache)) && $_caching != Smarty::CACHING_LIFETIME_CURRENT) {
|
||||
// check if compiled code can be merged (contains no variable part)
|
||||
if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2)
|
||||
and substr_count($include_file, '(') == 0 and substr_count($include_file, '$_smarty_tpl->') == 0) {
|
||||
$tpl_name = null;
|
||||
eval("\$tpl_name = $include_file;");
|
||||
if (!isset($compiler->smarty->merged_templates_func[$tpl_name]) || $compiler->inheritance) {
|
||||
$tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
|
||||
// save unique function name
|
||||
$compiler->smarty->merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'. str_replace('.', '_', uniqid('', true));
|
||||
// use current nocache hash for inlined code
|
||||
$compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
|
||||
if ($compiler->template->caching) {
|
||||
// needs code for cached page but no cache file
|
||||
$tpl->caching = self::CACHING_NOCACHE_CODE;
|
||||
}
|
||||
// make sure whole chain gest compiled
|
||||
$tpl->mustCompile = true;
|
||||
if (!($tpl->source->uncompiled) && $tpl->source->exists) {
|
||||
// get compiled code
|
||||
$compiled_code = $tpl->compiler->compileTemplate($tpl);
|
||||
// release compiler object to free memory
|
||||
unset($tpl->compiler);
|
||||
// merge compiled code for {function} tags
|
||||
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
|
||||
// merge filedependency
|
||||
$tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp,$tpl->source->type);
|
||||
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
|
||||
// remove header code
|
||||
$compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
|
||||
if ($tpl->has_nocache_code) {
|
||||
// replace nocache_hash
|
||||
$compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
|
||||
$compiler->template->has_nocache_code = true;
|
||||
}
|
||||
$compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
|
||||
$has_compiled_template = true;
|
||||
}
|
||||
} else {
|
||||
$has_compiled_template = true;
|
||||
if ($merge_compiled_includes || $compiler->inheritance) {
|
||||
// variable template name ?
|
||||
if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2))
|
||||
|| substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
|
||||
) {
|
||||
$merge_compiled_includes = false;
|
||||
if ($compiler->inheritance) {
|
||||
$compiler->trigger_template_error(' variable template file names not allow within {block} tags');
|
||||
}
|
||||
}
|
||||
// variable compile_id?
|
||||
if (isset($_attr['compile_id'])) {
|
||||
if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2))
|
||||
|| substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
|
||||
) {
|
||||
$merge_compiled_includes = false;
|
||||
if ($compiler->inheritance) {
|
||||
$compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
|
||||
$merge_compiled_includes = false;
|
||||
if ($compiler->inheritance) {
|
||||
$compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($merge_compiled_includes || $compiler->inheritance) {
|
||||
// we must observe different compile_id
|
||||
$uid = sha1($_compile_id);
|
||||
$tpl_name = null;
|
||||
$nocache = false;
|
||||
eval("\$tpl_name = $include_file;");
|
||||
if (!isset($compiler->smarty->merged_templates_func[$tpl_name][$uid]) || $compiler->inheritance) {
|
||||
$tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
|
||||
// save unique function name
|
||||
$compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
|
||||
// use current nocache hash for inlined code
|
||||
$compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
|
||||
if ($compiler->template->caching && $_caching == self::CACHING_NOCACHE_CODE) {
|
||||
// all code must be nocache
|
||||
$nocache = true;
|
||||
}
|
||||
if ($compiler->inheritance) {
|
||||
$tpl->compiler->inheritance = true;
|
||||
}
|
||||
// make sure whole chain gets compiled
|
||||
$tpl->mustCompile = true;
|
||||
if (!($tpl->source->uncompiled) && $tpl->source->exists) {
|
||||
|
||||
|
||||
// get compiled code
|
||||
$compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
|
||||
// release compiler object to free memory
|
||||
unset($tpl->compiler);
|
||||
// merge compiled code for {function} tags
|
||||
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
|
||||
// merge filedependency
|
||||
$tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp, $tpl->source->type);
|
||||
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
|
||||
// remove header code
|
||||
$compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
|
||||
if ($tpl->has_nocache_code) {
|
||||
// replace nocache_hash
|
||||
$compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
|
||||
$compiler->template->has_nocache_code = true;
|
||||
}
|
||||
$compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
|
||||
$has_compiled_template = true;
|
||||
unset ($tpl);
|
||||
}
|
||||
} else {
|
||||
$has_compiled_template = true;
|
||||
}
|
||||
}
|
||||
// delete {include} standard attributes
|
||||
unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
|
||||
@@ -175,7 +214,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
foreach ($_attr as $key => $value) {
|
||||
$_pairs[] = "'$key'=>$value";
|
||||
}
|
||||
$_vars = 'array('.join(',',$_pairs).')';
|
||||
$_vars = 'array(' . join(',', $_pairs) . ')';
|
||||
$_has_vars = true;
|
||||
} else {
|
||||
$compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
|
||||
@@ -185,19 +224,21 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
$_has_vars = false;
|
||||
}
|
||||
if ($has_compiled_template) {
|
||||
$_hash = $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'];
|
||||
// never call inline templates in nocache mode
|
||||
$compiler->suppressNocacheProcessing = true;
|
||||
$_hash = $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'];
|
||||
$_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
|
||||
$_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
|
||||
$_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
|
||||
if (isset($_assign)) {
|
||||
$_output .= 'ob_start(); ';
|
||||
}
|
||||
$_output .= $compiler->smarty->merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n";
|
||||
$_output .= $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] . "(\$_smarty_tpl);\n";
|
||||
$_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
|
||||
if (isset($_assign)) {
|
||||
$_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
|
||||
}
|
||||
$_output .= "/* End of included template \"" . $tpl_name . "\" */?>";
|
||||
$_output .= "\n/* End of included template \"" . $tpl_name . "\" */?>";
|
||||
|
||||
return $_output;
|
||||
}
|
||||
@@ -211,5 +252,4 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
|
||||
return $_output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user