- move $smarty->append() and $smarty->appendByRef() into extension

This commit is contained in:
Uwe Tews
2015-06-28 05:22:19 +02:00
parent 3442acd407
commit ce0c1dcfae
5 changed files with 110 additions and 64 deletions

View File

@@ -5,6 +5,7 @@
- move auto load filter methods into extension - move auto load filter methods into extension
- move $smarty->getTemplateVars() into extension - move $smarty->getTemplateVars() into extension
- move getStreamVariable() into extension - move getStreamVariable() into extension
- move $smarty->append() and $smarty->appendByRef() into extension
27.06.2015 27.06.2015
- bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64 - bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64

View File

@@ -82,6 +82,7 @@ class Smarty_Autoloader
'smarty_internal_extension_loadplugin' => true, 'smarty_internal_extension_loadplugin' => true,
'smarty_internal_extension_clearcompiled' => true, 'smarty_internal_extension_clearcompiled' => true,
'smarty_internal_extension_getvars' => true, 'smarty_internal_extension_getvars' => true,
'smarty_internal_extension_append' => true,
'smarty_internal_filter_handler' => true, 'smarty_internal_filter_handler' => true,
'smarty_internal_function_call_handler' => true, 'smarty_internal_function_call_handler' => true,
'smarty_internal_cacheresource_file' => true, 'smarty_internal_cacheresource_file' => true,

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.28-dev/15'; const SMARTY_VERSION = '3.1.28-dev/16';
/** /**
* define variable scopes * define variable scopes

View File

@@ -121,53 +121,7 @@ class Smarty_Internal_Data
*/ */
public function append($tpl_var, $value = null, $merge = false, $nocache = false) public function append($tpl_var, $value = null, $merge = false, $nocache = false)
{ {
if (is_array($tpl_var)) { Smarty_Internal_Extension_Append::append($this, $tpl_var, $value, $merge, $nocache);
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if ($_key != '') {
if (!isset($this->tpl_vars[$_key])) {
$tpl_var_inst = $this->getVariable($_key, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$this->tpl_vars[$_key] = new Smarty_Variable(null, $nocache);
} else {
$this->tpl_vars[$_key] = clone $tpl_var_inst;
}
}
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)) {
foreach ($_val as $_mkey => $_mval) {
$this->tpl_vars[$_key]->value[$_mkey] = $_mval;
}
} else {
$this->tpl_vars[$_key]->value[] = $_val;
}
}
}
} else {
if ($tpl_var != '' && isset($value)) {
if (!isset($this->tpl_vars[$tpl_var])) {
$tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
} else {
$this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
}
}
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)) {
foreach ($value as $_mkey => $_mval) {
$this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
}
} else {
$this->tpl_vars[$tpl_var]->value[] = $value;
}
}
}
return $this; return $this;
} }
@@ -182,22 +136,7 @@ class Smarty_Internal_Data
*/ */
public function appendByRef($tpl_var, &$value, $merge = false) public function appendByRef($tpl_var, &$value, $merge = false)
{ {
if ($tpl_var != '' && isset($value)) { Smarty_Internal_Extension_Append::appendByRef($this, $tpl_var, $value, $merge);
if (!isset($this->tpl_vars[$tpl_var])) {
$this->tpl_vars[$tpl_var] = new Smarty_Variable();
}
if (!is_array($this->tpl_vars[$tpl_var]->value)) {
settype($this->tpl_vars[$tpl_var]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_key => $_val) {
$this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
}
} else {
$this->tpl_vars[$tpl_var]->value[] = &$value;
}
}
return $this; return $this;
} }

View File

@@ -0,0 +1,105 @@
<?php
/**
* Smarty Extension Append
*
* getStreamVariable() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Extension_Append
{
/**
* appends values to template variables
*
* @param array|string $tpl_var the template variable name(s)
* @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
*
* @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
*/
public static function append($obj, $tpl_var, $value, $merge, $nocache)
{
if (is_array($tpl_var)) {
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if ($_key != '') {
if (!isset($obj->tpl_vars[$_key])) {
$tpl_var_inst = Smarty_Internal_Extension_GetVars::getVariable($obj, $_key, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$obj->tpl_vars[$_key] = new Smarty_Variable(null, $nocache);
} else {
$obj->tpl_vars[$_key] = clone $tpl_var_inst;
}
}
if (!(is_array($obj->tpl_vars[$_key]->value) || $obj->tpl_vars[$_key]->value instanceof ArrayAccess)) {
settype($obj->tpl_vars[$_key]->value, 'array');
}
if ($merge && is_array($_val)) {
foreach ($_val as $_mkey => $_mval) {
$obj->tpl_vars[$_key]->value[$_mkey] = $_mval;
}
} else {
$obj->tpl_vars[$_key]->value[] = $_val;
}
}
}
} else {
if ($tpl_var != '' && isset($value)) {
if (!isset($obj->tpl_vars[$tpl_var])) {
$tpl_var_inst = Smarty_Internal_Extension_GetVars::getVariable($obj, $tpl_var, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$obj->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
} else {
$obj->tpl_vars[$tpl_var] = clone $tpl_var_inst;
}
}
if (!(is_array($obj->tpl_vars[$tpl_var]->value) || $obj->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
settype($obj->tpl_vars[$tpl_var]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_mkey => $_mval) {
$obj->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
}
} else {
$obj->tpl_vars[$tpl_var]->value[] = $value;
}
}
}
return $obj;
}
/**
* appends values to template variables by reference
*
* @param string $tpl_var the template variable name
* @param mixed &$value the referenced value to append
* @param boolean $merge flag if array elements shall be merged
*
* @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
*/
public static function appendByRef($obj, $tpl_var, &$value, $merge)
{
if ($tpl_var != '' && isset($value)) {
if (!isset($obj->tpl_vars[$tpl_var])) {
$obj->tpl_vars[$tpl_var] = new Smarty_Variable();
}
if (!is_array($obj->tpl_vars[$tpl_var]->value)) {
settype($obj->tpl_vars[$tpl_var]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_key => $_val) {
$obj->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
}
} else {
$obj->tpl_vars[$tpl_var]->value[] = &$value;
}
}
return $obj;
}
}