Skip to content

Commit

Permalink
Merge pull request #7 from uwetews/master
Browse files Browse the repository at this point in the history
-bugfix and enhancement on subtemplate {include} and template {function}
  • Loading branch information
uwetews committed Nov 1, 2014
2 parents 337ab5b + bc13ebb commit 4369b2f
Show file tree
Hide file tree
Showing 14 changed files with 447 additions and 299 deletions.
6 changes: 6 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
===== 3.1.22-dev ===== (xx.xx.2014)
01.11.2014
-bugfix and enhancement on subtemplate {include} and template {function} tags.
* Calling a template which has a nocache section could fail if it was called from a cached and a not cached subtemplate.
* Calling the same subtemplate cached and not cached with the $smarty->merge_compiled_includes enabled could cause problems
* Many smaller related changes

30.10.2014
- bugfix access to class constant by object like {$object::CONST} or variable class name {$class::CONST} did not work (forum 25301)

Expand Down
35 changes: 11 additions & 24 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Project: Smarty: the PHP compiling template engine
* File: Smarty.class.php
*
* SVN: $Id: Smarty.class.php 4897 2014-10-14 22:29:58Z Uwe.Tews@googlemail.com $
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
Expand All @@ -24,7 +24,7 @@
* @author Uwe Tews
* @author Rodney Rehm
* @package Smarty
* @version 3.1.22-dev
* @version 3.1-DEV
*/

/**
Expand Down Expand Up @@ -110,8 +110,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = 'Smarty-3.1.22-dev/1';

const SMARTY_VERSION = 'Smarty-3.1.21-dev/2';

/**
* define variable scopes
Expand Down Expand Up @@ -493,12 +492,6 @@ class Smarty extends Smarty_Internal_TemplateBase

/**#@-*/

/**
* global template functions
*
* @var array
*/
public $template_functions = array();
/**
* resource type used if none given
* Must be an valid key of $registered_resources.
Expand Down Expand Up @@ -657,19 +650,13 @@ class Smarty extends Smarty_Internal_TemplateBase
* @var bool
*/
public $_parserdebug = false;
/**
* Saved parameter of merged templates during compilation
*
* @var array
*/
public $merged_templates_func = array();

/**
* Cache of is_file results of loadPlugin()
*
*
* @var array
*/
public static $_is_file_cache= array();
public static $_is_file_cache = array();

/**#@-*/

Expand All @@ -687,10 +674,10 @@ public function __construct()
$this->start_time = microtime(true);
// set default dirs
$this->setTemplateDir('.' . DS . 'templates' . DS)
->setCompileDir('.' . DS . 'templates_c' . DS)
->setPluginsDir(SMARTY_PLUGINS_DIR)
->setCacheDir('.' . DS . 'cache' . DS)
->setConfigDir('.' . DS . 'configs' . DS);
->setCompileDir('.' . DS . 'templates_c' . DS)
->setPluginsDir(SMARTY_PLUGINS_DIR)
->setCacheDir('.' . DS . 'cache' . DS)
->setConfigDir('.' . DS . 'configs' . DS);

$this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';
if (isset($_SERVER['SCRIPT_NAME'])) {
Expand Down Expand Up @@ -986,8 +973,8 @@ public function setConfigDir($config_dir)
/**
* Add config directory(s)
*
* @param string|array $config_dir directory(s) of config sources
* @param mixed $key key of the array element to assign the config dir to
* @param string|array $config_dir directory(s) of config sources
* @param mixed $key key of the array element to assign the config dir to
*
* @return Smarty current Smarty instance for chaining
*/
Expand Down
20 changes: 15 additions & 5 deletions libs/sysplugins/smarty_cacheresource.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,7 @@ public function __construct(Smarty_Internal_Template $_template)
if ($smarty->debugging) {
Smarty_Internal_Debug::start_cache($_template);
}
if ($handler->process($_template, $this) === false) {
$this->valid = false;
} else {
$this->processed = true;
}
$this->process($_template);
if ($smarty->debugging) {
Smarty_Internal_Debug::end_cache($_template);
}
Expand All @@ -413,6 +409,20 @@ public function __construct(Smarty_Internal_Template $_template)
}
}

/**
* Process cached template
*
* @param Smarty_Internal_Template $_template template object
*/
public function process(Smarty_Internal_Template $_template)
{
if ($this->handler->process($_template, $this) === false) {
$this->valid = false;
} else {
$this->processed = true;
}
}

/**
* Write this cache object to handler
*
Expand Down
17 changes: 7 additions & 10 deletions libs/sysplugins/smarty_internal_compile_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static function compileChildBlock($compiler, $_name = null)
// if called by {$smarty.block.child} we must search the name of enclosing {block}
if ($_name == null) {
$stack_count = count($compiler->_tag_stack);
while (--$stack_count >= 0) {
while (-- $stack_count >= 0) {
if ($compiler->_tag_stack[$stack_count][0] == 'block') {
$_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'], "\"'");
break;
Expand Down Expand Up @@ -173,20 +173,18 @@ static function compileChildBlock($compiler, $_name = null)
$_tpl->compiler->suppressHeader = true;
$_tpl->compiler->suppressFilter = true;
$_tpl->compiler->suppressTemplatePropertyHeader = true;
$_tpl->compiler->suppressMergedTemplates = true;
$nocache = $compiler->nocache || $compiler->tag_nocache;
if (strpos($compiler->template->block_data[$_name]['source'], self::parent) !== false) {
$_output = str_replace(self::parent, $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl, $nocache));
$_output = str_replace(self::parent, $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl, $nocache, $compiler->parent_compiler));
} elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
$_output = $_tpl->compiler->compileTemplate($_tpl, $nocache) . $compiler->parser->current_buffer->to_smarty_php();
$_output = $_tpl->compiler->compileTemplate($_tpl, $nocache, $compiler->parent_compiler) . $compiler->parser->current_buffer->to_smarty_php();
} elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
$_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl, $nocache);
$_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl, $nocache, $compiler->parent_compiler);
} elseif (!empty($compiler->template->block_data[$_name])) {
$_output = $_tpl->compiler->compileTemplate($_tpl, $nocache);
$_output = $_tpl->compiler->compileTemplate($_tpl, $nocache, $compiler->parent_compiler);
}
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
$compiler->merged_templates = array_merge($compiler->merged_templates, $_tpl->compiler->merged_templates);
$compiler->template->properties['tpl_function'] = array_merge($compiler->template->properties['tpl_function'], $_tpl->properties['tpl_function']);
$compiler->template->variable_filters = $_tpl->variable_filters;
if ($_tpl->has_nocache_code) {
$compiler->template->has_nocache_code = true;
Expand Down Expand Up @@ -221,7 +219,7 @@ static function compileParentBlock($compiler, $_name = null)
// if called by {$smarty.block.parent} we must search the name of enclosing {block}
if ($_name == null) {
$stack_count = count($compiler->_tag_stack);
while (--$stack_count >= 0) {
while (-- $stack_count >= 0) {
if ($compiler->_tag_stack[$stack_count][0] == 'block') {
$_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'], "\"'");
break;
Expand All @@ -245,7 +243,6 @@ static function compileParentBlock($compiler, $_name = null)
*
* @param $compiler
* @param string $source source text
*
*/
static function blockSource($compiler, $source)
{
Expand Down
54 changes: 6 additions & 48 deletions libs/sysplugins/smarty_internal_compile_call.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ public function compile($args, $compiler)
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr['assign'];
}
$_name = $_attr['name'];
if ($compiler->compiles_template_function) {
$compiler->called_functions[] = trim($_name, "'\"");
}
$_name = trim($_attr['name'], "'\"");
unset($_attr['name'], $_attr['assign'], $_attr['nocache']);
// set flag (compiled code of {function} must be included in cache file
if ($compiler->nocache || $compiler->tag_nocache) {
if (!$compiler->template->caching || $compiler->nocache || $compiler->tag_nocache) {
$compiler->parent_compiler->templateProperties['tpl_function']['to_cache'][$_name] = true;
$_nocache = 'true';
} else {
$_nocache = 'false';
Expand All @@ -74,54 +72,14 @@ public function compile($args, $compiler)
$_paramsArray[] = "'$_key'=>$_value";
}
}
if (isset($compiler->template->properties['function'][$_name]['parameter'])) {
foreach ($compiler->template->properties['function'][$_name]['parameter'] as $_key => $_value) {
if (!isset($_attr[$_key])) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
}
} elseif (isset($compiler->smarty->template_functions[$_name]['parameter'])) {
foreach ($compiler->smarty->template_functions[$_name]['parameter'] as $_key => $_value) {
if (!isset($_attr[$_key])) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
}
}
//variable name?
if (!(strpos($_name, '$') === false)) {
$call_cache = $_name;
$call_function = '$tmp = "smarty_template_function_".' . $_name . '; $tmp';
} else {
$_name = trim($_name, "'\"");
$call_cache = "'{$_name}'";
$call_function = 'smarty_template_function_' . $_name;
}

$_params = 'array(' . implode(",", $_paramsArray) . ')';
$_hash = str_replace('-', '_', $compiler->template->properties['nocache_hash']);
//$compiler->suppressNocacheProcessing = true;
// was there an assign attribute
if (isset($_assign)) {
if ($compiler->template->caching) {
$_output = "<?php ob_start(); Smarty_Internal_Function_Call_Handler::call ({$call_cache},\$_smarty_tpl,{$_params},'{$_hash}',{$_nocache}); \$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
} else {
$_output = "<?php ob_start(); {$call_function}(\$_smarty_tpl,{$_params}); \$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
}
$_output = "<?php ob_start();\$_smarty_tpl->callTemplateFunction ('{$_name}', \$_smarty_tpl, {$_params}, {$_nocache}); \$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
} else {
if ($compiler->template->caching) {
$_output = "<?php Smarty_Internal_Function_Call_Handler::call ({$call_cache},\$_smarty_tpl,{$_params},'{$_hash}',{$_nocache});?>\n";
} else {
$_output = "<?php {$call_function}(\$_smarty_tpl,{$_params});?>\n";
}
$_output = "<?php \$_smarty_tpl->callTemplateFunction ('{$_name}', \$_smarty_tpl, {$_params}, {$_nocache});?>\n";
}

return $_output;
}
}
Loading

0 comments on commit 4369b2f

Please sign in to comment.