- fixed scope problem with parent variables when appending variables within templates.

- fixed code for {block} without childs (possible sources for notice errors removed)
This commit is contained in:
Uwe.Tews
2009-04-12 22:26:02 +00:00
parent bdb43faab3
commit 139b3d4570
5 changed files with 62 additions and 37 deletions

View File

@@ -1,3 +1,7 @@
04/13/2009
- fixed scope problem with parent variables when appending variables within templates.
- fixed code for {block} without childs (possible sources for notice errors removed)
04/12/2009
- added append and prepend attribute to {block} tag

View File

@@ -34,12 +34,14 @@ class Smarty_Internal_Compile_BlockClose extends Smarty_Internal_CompileBase {
$this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"');
}
$_name = trim($saved_data[0]['name'], "'");
if ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
$_output = $compiler->template->block_data[$_name]['compiled'].$compiler->template->extracted_compiled_code;
} elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
$_output = $compiler->template->extracted_compiled_code.$compiler->template->block_data[$_name]['compiled'];
} elseif (!empty($compiler->template->block_data[$_name])) {
$_output = $compiler->template->block_data[$_name]['compiled'];
if (isset($compiler->template->block_data[$_name])) {
if ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
$_output = $compiler->template->block_data[$_name]['compiled'] . $compiler->template->extracted_compiled_code;
} elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
$_output = $compiler->template->extracted_compiled_code . $compiler->template->block_data[$_name]['compiled'];
} elseif (!empty($compiler->template->block_data[$_name])) {
$_output = $compiler->template->block_data[$_name]['compiled'];
}
} else {
$_output = $compiler->template->extracted_compiled_code;
}

View File

@@ -53,25 +53,29 @@ class Smarty_Internal_Compile_Extend extends Smarty_Internal_CompileBase {
$tpl->suppressHeader = false;
$_name = trim($_match[3], "\"'");
if ($this->compiler->template->block_data[$_name]['mode'] == 'prepend') {
$this->compiler->template->block_data[$_name]['compiled'] .= $compiled_content;
$this->compiler->template->block_data[$_name]['source'] .= $matches[3];
} elseif ($this->compiler->template->block_data[$_name]['mode'] == 'append') {
$this->compiler->template->block_data[$_name]['compiled'] = $compiled_content . $this->compiler->template->block_data[$_name]['compiled'];
$this->compiler->template->block_data[$_name]['source'] = $matches[3] . $this->compiler->template->block_data[$_name]['source'];
} elseif (!isset($this->compiler->template->block_data[$_name])) {
if (isset($this->compiler->template->block_data[$_name])) {
if ($this->compiler->template->block_data[$_name]['mode'] == 'prepend') {
$this->compiler->template->block_data[$_name]['compiled'] .= $compiled_content;
$this->compiler->template->block_data[$_name]['source'] .= $matches[3];
} elseif ($this->compiler->template->block_data[$_name]['mode'] == 'append') {
$this->compiler->template->block_data[$_name]['compiled'] = $compiled_content . $this->compiler->template->block_data[$_name]['compiled'];
$this->compiler->template->block_data[$_name]['source'] = $matches[3] . $this->compiler->template->block_data[$_name]['source'];
}
} else {
$this->compiler->template->block_data[$_name]['compiled'] = $compiled_content;
$this->compiler->template->block_data[$_name]['source'] = $matches[3];
}
if ($this->compiler->template->block_data[$_name]['mode'] != 'replace') {
if (preg_match('/(.?)(append=true)(.*)/', $matches[2], $_match) != 0) {
$this->compiler->template->block_data[$_name]['mode'] = 'append';
} elseif (preg_match('/(.?)(prepend=true)(.*)/', $matches[2], $_match) != 0) {
$this->compiler->template->block_data[$_name]['mode'] = 'prepend';
} else {
$this->compiler->template->block_data[$_name]['mode'] = 'replace';
if (isset($this->compiler->template->block_data[$_name]['mode'])) {
if ($this->compiler->template->block_data[$_name]['mode'] != 'replace') {
if (preg_match('/(.?)(append=true)(.*)/', $matches[2], $_match) != 0) {
$this->compiler->template->block_data[$_name]['mode'] = 'append';
} elseif (preg_match('/(.?)(prepend=true)(.*)/', $matches[2], $_match) != 0) {
$this->compiler->template->block_data[$_name]['mode'] = 'prepend';
}
}
}
} else {
$this->compiler->template->block_data[$_name]['mode'] = 'replace';
}
}
}
}

View File

@@ -521,7 +521,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
$_filepath = $_template_dir . $file;
if (file_exists($_filepath))
return $_filepath;
}
}
if (file_exists($file)) return $file;
// no tpl file found
if (!empty($this->smarty->default_template_handler_func)) {

View File

@@ -20,7 +20,7 @@ class Smarty_Internal_TemplateBase {
* @param array $ |string $tpl_var the template variable name(s)
* @param mixed $value the value to assign
* @param boolean $nocache if true any output of this variable will be not cached
* @param boolean $scope the scope the variable will have (local,parent or root)
* @param boolean $scope the scope the variable will have (local,parent or root)
*/
public function assign($tpl_var, $value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{
@@ -59,7 +59,7 @@ class Smarty_Internal_TemplateBase {
* @param string $tpl_var the template variable name
* @param mixed $ &$value the referenced value to assign
* @param boolean $nocache if true any output of this variable will be not cached
* @param boolean $scope the scope the variable will have (local,parent or root)
* @param boolean $scope the scope the variable will have (local,parent or root)
*/
public function assign_by_ref($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{
@@ -76,7 +76,7 @@ class Smarty_Internal_TemplateBase {
* @param mixed $value the value to append
* @param boolean $merge flag if array elements shall be merged
* @param boolean $nocache if true any output of this variable will be not cached
* @param boolean $scope the scope the variable will have (local,parent or root)
* @param boolean $scope the scope the variable will have (local,parent or root)
*/
public function append($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{
@@ -86,9 +86,17 @@ class Smarty_Internal_TemplateBase {
if ($_key != '') {
if (!isset($this->tpl_vars[$_key])) {
$this->check_tplvar($_key);
$this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $scope);
$tpl_var_inst = $this->getVariable($_key, null, true, false);
if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
$this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $scope);
} else {
$this->tpl_vars[$_key] = clone $tpl_var_inst;
if ($scope != SMARTY_LOCAL_SCOPE) {
$this->tpl_vars[$_key]->scope = $scope;
}
}
}
if (!is_array($this->tpl_vars[$_key]->value)) {
if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
settype($this->tpl_vars[$_key]->value, 'array');
}
if ($merge && is_array($_val)) {
@@ -104,9 +112,17 @@ class Smarty_Internal_TemplateBase {
if ($tpl_var != '' && isset($value)) {
if (!isset($this->tpl_vars[$tpl_var])) {
$this->check_tplvar($tpl_var);
$this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope);
$tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
$this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope);
} else {
$this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
if ($scope != SMARTY_LOCAL_SCOPE) {
$this->tpl_vars[$tpl_var]->scope = $scope;
}
}
}
if (!is_array($this->tpl_vars[$tpl_var]->value)) {
if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
settype($this->tpl_vars[$tpl_var]->value, 'array');
}
if ($merge && is_array($value)) {
@@ -119,6 +135,7 @@ class Smarty_Internal_TemplateBase {
}
}
}
/**
* appends values to template variables by reference
*
@@ -189,12 +206,11 @@ class Smarty_Internal_TemplateBase {
* @param boolean $search_parents search also in parent data
* @return object the object of the variable
*/
public function getVariable($variable, $_ptr = null, $search_parents = true)
public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
{
if ($_ptr === null) {
$_ptr = $this;
}
while ($_ptr !== null) {
} while ($_ptr !== null) {
if (isset($_ptr->tpl_vars[$variable])) {
// found it, return it
return $_ptr->tpl_vars[$variable];
@@ -211,7 +227,7 @@ class Smarty_Internal_TemplateBase {
// found it, return it
return $_ptr->global_tpl_vars[$variable];
}
if (Smarty::$error_unassigned) {
if (Smarty::$error_unassigned && $error_enable) {
throw new Exception('Undefined Smarty variable "' . $variable . '"');
} else {
return new Undefined_Smarty_Variable;
@@ -297,8 +313,8 @@ class Smarty_Internal_TemplateBase {
* @returns string a unique template id
*/
public function buildTemplateId ($_resource, $_cache_id, $_compile_id)
{
// return md5($_resource . md5($_cache_id) . md5($_compile_id));
{
// return md5($_resource . md5($_cache_id) . md5($_compile_id));
return crc32($_resource . $_cache_id . $_compile_id);
}
@@ -329,7 +345,6 @@ class Smarty_Data extends Smarty_Internal_TemplateBase {
public $parent = null;
// config vars
public $config_vars = array();
/**
* create Smarty data object
*/
@@ -363,7 +378,7 @@ class Smarty_Variable {
*
* @param mixed $value the value to assign
* @param boolean $nocache if true any output of this variable will be not cached
* @param boolean $scope the scope the variable will have (local,parent or root)
* @param boolean $scope the scope the variable will have (local,parent or root)
*/
public function __construct ($value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{