- improvements and bugfix on {include} tag handling

******************************************************************
NOTICE: existing compiled template and cache files must be deleted
******************************************************************
This commit is contained in:
Uwe.Tews
2009-09-23 16:50:16 +00:00
parent 5bf063d2cc
commit 324276f7fa
4 changed files with 69 additions and 34 deletions

View File

@@ -1,3 +1,7 @@
09/23/2009
- improvements and bugfix on {include} tag handling
NOTICE: existing compiled template and cache files must be deleted
09/19/2009
- replace internal "eval()" calls by "include" during rendering process
- speed improvment for templates which have included subtemplates

View File

@@ -28,16 +28,42 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
// check and get attributes
$_attr = $this->_get_attributes($args);
// save posible attributes
$include_file = $_attr['file'];
// check if compiled code can be merged
if (strpos($include_file, '$_smarty_tpl') === false) {
$tpl = new Smarty_Template (trim($include_file, "'\""), $compiler->smarty, $compiler->template);
$compiled_tpl = $tpl->getCompiledTemplate() . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>";
$compiler->template->properties['file_dependency']['F' . abs(crc32($tpl->getTemplateFilepath()))] = array($tpl->getTemplateFilepath(), $tpl->getTemplateTimestamp());
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
$has_compiled_template = true;
} else {
$has_compiled_template = false;
$include_file = $_attr['file'];
$has_compiled_template = false;
if (true) {
// check if compiled code can be merged
if (strpos($include_file, '$_smarty_tpl') === false) {
$tpl = $compiler->smarty->createTemplate (trim($include_file, "'\""), $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
do {
$must_compile = false;
$prop = array();
$compiled_tpl = $tpl->getCompiledTemplate();
preg_match('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>)/', $compiled_tpl, $matches); //var_dump($matches, $compiled_tpl);
if (isset($matches[2])) {
$prop = unserialize($matches[2]);
foreach ($prop['file_dependency'] as $_file_to_check) {
If (is_file($_file_to_check[0])) {
$mtime = filemtime($_file_to_check[0]);
} else {
$tpl->parseResourceName($_file_to_check[0], $resource_type, $resource_name, $resource_handler);
$mtime = $resource_handler->getTemplateTimestampTypeName($resource_type, $resource_name);
}
If ($mtime != $_file_to_check[1]) {
$must_compile = true;
break;
}
}
if ($must_compile) {
// recompile
$tpl->compileTemplateSource();
}
}
} while ($must_compile);
if (isset($prop['file_dependency'])) {
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $prop['file_dependency']);
}
$has_compiled_template = true;
}
}
if (isset($_attr['assign'])) {
@@ -56,11 +82,11 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
}
}
// default for included templates
// if ($compiler->template->caching) {
// $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
// } else {
$_caching = SMARTY_CACHING_OFF;
// }
if ($compiler->template->caching) {
$_caching = SMARTY_CACHING_LIFETIME_CURRENT;
} else {
$_caching = SMARTY_CACHING_OFF;
}
/*
* if the {include} tag provides individual parameter for caching
* it will not be included into the common cache file and treated like
@@ -108,9 +134,9 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>";
} else {
if ($has_compiled_template) {
$_output .= " \$_tmp = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
$_output .= $compiled_tpl;
$_output .= "<?php \$_smarty_tpl = \$_tmp;?>";
$_output .= " \$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
$_output .= $compiled_tpl . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>";;
$_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
} else {
$_output .= " echo \$_smarty_tpl->smarty->fetch(\$_template); ?>";
}

View File

@@ -100,11 +100,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
if (!$this->parseResourceName ($template_resource, $this->resource_type, $this->resource_name, $dummy)) {
throw new Exception ("Unable to parse resource name \"{$template_resource}\"");
}
// load cacher
if ($this->caching) {
$this->smarty->loadPlugin($this->cacher_class);
$this->cacher_object = new $this->cacher_class($this->smarty);
}
// load cache resource
if (!$this->isEvaluated() && $this->caching && !isset($this->smarty->cache_resource_objects[$this->caching_type])) {
$this->smarty->loadPlugin($this->cache_resource_class);
@@ -261,7 +256,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
*/
public function compileTemplateSource ()
{
$_start_time = $this->_get_time();
$_start_time = $this->_get_time();
if (!$this->isEvaluated) {
$this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
}
// compile template
if (!is_object($this->compiler_object)) {
// load compiler
@@ -270,7 +268,12 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// $this->smarty->loadPlugin('Smarty_Internal_CompileBase');
// $this->smarty->loadPlugin('Smarty_Internal_TemplateCompilerBase');
$this->smarty->loadPlugin($this->resource_objects[$this->resource_type]->compiler_class);
$this->compiler_object = new $this->resource_objects[$this->resource_type]->compiler_class($this->resource_objects[$this->resource_type]->template_lexer_class, $this->resource_objects[$this->resource_type]->template_parser_class, $this->smarty);
$this->compiler_object = new $this->resource_objects[$this->resource_type]->compiler_class($this->resource_objects[$this->resource_type]->template_lexer_class, $this->resource_objects[$this->resource_type]->template_parser_class, $this->smarty);
// load cacher
if ($this->caching) {
$this->smarty->loadPlugin($this->cacher_class);
$this->cacher_object = new $this->cacher_class($this->smarty);
}
}
if (!is_object($this->smarty->write_file_object)) {
require_once(SMARTY_SYSPLUGINS_DIR . 'internal.write_file.php');
@@ -282,9 +285,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// compiling succeded
if (!$this->isEvaluated()) {
// build template property string
$this->properties_string = "<?php \$_smarty_tpl->decodeProperties('" . str_replace("'", '"', (serialize($this->properties))) . "'); ?>\n";
$this->properties_string = "<?php \$_smarty_tpl->decodeProperties('" . str_replace("'", '"', (serialize($this->properties))) . "'); ?>\n";
$this->compiled_template = $this->dir_acc_sec_string . $this->properties_string . $this->compiled_template;
// write compiled template
$this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->dir_acc_sec_string . $this->properties_string . $this->getCompiledTemplate());
$this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->compiled_template);
// make template and compiled file timestamp match
touch($this->getCompiledFilepath(), $this->getTemplateTimestamp());
}
@@ -464,14 +468,15 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
$this->render_time += $this->_get_time() - $_start_time;
$this->rendered_content = ob_get_clean();
if (!$this->isEvaluated) {
$this->properties['file_dependency']['F'.abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
$this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
}
if ($this->parent instanceof Smarty_Template or $this->parent instanceof Smarty_Internal_Template) {
// var_dump('merge ', $this->parent->getTemplateFilepath(), $this->parent->properties['file_dependency'], $this->getTemplateFilepath(), $this->properties['file_dependency']);
$this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']);
}
// write to cache when nessecary
if (!$this->isEvaluated() && $this->caching) {
//$this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
// $this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
// write rendered template
$this->writeCachedContent($this);
if ($this->usesCompiler()) {
@@ -624,8 +629,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
$this->properties['cache_lifetime'] = $prop['cache_lifetime'];
}
if (isset($prop['file_dependency'])) {
$this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $prop['file_dependency']);
//$this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
$this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $prop['file_dependency']);
// $this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
}
if (!empty($prop['function'])) {
foreach ($prop['function'] as $_name => $_data) {

View File

@@ -296,12 +296,12 @@ class Smarty_Internal_TemplateBase {
// we got a template resource
$_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id);
// already in template cache?
if (isset($this->template_objects[$_templateId])) {
if (isset($this->smarty->template_objects[$_templateId])) {
// return cached template object
return $this->template_objects[$_templateId];
return $this->smarty->template_objects[$_templateId];
} else {
// create and cache new template object
return new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
return new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id);
}
} else {
// just return a copy of template class