- added global variable scope SMARTY_GLOBAL_SCOPE

- enable 'variable' filter by default
This commit is contained in:
Uwe.Tews
2009-04-10 12:33:51 +00:00
parent 33082844a5
commit eace27e2b6
10 changed files with 521 additions and 540 deletions

View File

@@ -1,4 +1,6 @@
04/10/2009
- added global variable scope SMARTY_GLOBAL_SCOPE
- enable 'variable' filter by default
- fixed {$smarty.block.parent.foo}
- implementation of a 'variable' filter as replacement for default modifier

View File

@@ -45,6 +45,7 @@ if (!defined('SMARTY_DIR')) {
define('SMARTY_LOCAL_SCOPE', 0);
define('SMARTY_PARENT_SCOPE', 1);
define('SMARTY_ROOT_SCOPE', 2);
define('SMARTY_GLOBAL_SCOPE', 3);
/**
* load required base class for creation of the smarty object
@@ -156,12 +157,12 @@ class Smarty extends Smarty_Internal_TemplateBase {
// autoload filter
public $autoload_filters = array();
// status of filter on variable output
public $variable_filter = false;
public $variable_filter = true;
// cache resorce objects
public $cache_resource_objects = array();
// write file object
public $write_file_object = null;
// global smarty vars
// global internal smarty vars
public $_smarty_vars = array();
// start time for execution time calculation
public $start_time = 0;

View File

@@ -41,7 +41,13 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase {
$_attr = $this->_get_attributes($args);
if (isset($_attr['scope'])) {
$_scope = trim($_attr['scope'],'\'');
if ($_attr['scope'] == '\'parent\'') {
$_scope = SMARTY_PARENT_SCOPE;
} elseif ($_attr['scope'] == '\'root\'') {
$_scope = SMARTY_ROOT_SCOPE;
} elseif ($_attr['scope'] == '\'global\'') {
$_scope = SMARTY_GLOBAL_SCOPE;
}
}
if (isset($_attr['index'])) {

View File

@@ -1,47 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Assign
*
* Compiles the {assign} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Assign Global Class
*/
class Smarty_Internal_Compile_Assign_Global extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {assign_global} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @return string compiled code
*/
public function compile($args, $compiler)
{
$this->compiler = $compiler;
$this->required_attributes = array('var', 'value');
$this->optional_attributes = array('nocache');
$_nocache = 'false';
// check for nocache attribute before _get_attributes because
// it shall not controll caching of the compiled code, but is a parameter
if (isset($args['nocache'])) {
if ($args['nocache'] == 'true') {
$_nocache = 'true';
$_nocache_boolean = true;
}
unset($args['nocache']);
}
// check and get attributes
$_attr = $this->_get_attributes($args);
// compiled output
return "<?php \$_smarty_tpl->smarty->assign_global($_attr[var],$_attr[value],$_nocache);?>";
}
}
?>

View File

@@ -40,6 +40,8 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_parent_scope = SMARTY_PARENT_SCOPE;
} elseif ($_attr['scope'] == '\'root\'') {
$_parent_scope = SMARTY_ROOT_SCOPE;
} elseif ($_attr['scope'] == '\'global\'') {
$_parent_scope = SMARTY_GLOBAL_SCOPE;
}
}

View File

@@ -80,8 +80,6 @@ class Smarty_Internal_Compile_Smarty extends Smarty_Internal_CompileBase {
case 'config':
return "\$_smarty_tpl->getConfigVariable($_index[1])";
case 'global':
return "\$_smarty_tpl->smarty->getGlobalVariable($_index[1])->value";
case 'block':
if ($_index[1] == '\'parent\'') {
return "'" . addcslashes($compiler->template->block_data[trim($_index[2], "'")]['source'], "'") . "'";

View File

@@ -553,17 +553,34 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
} else {
// create variable in parent
$this->parent->tpl_vars[$_key] = clone $_value;
$this->smarty->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) {
if (isset($this->smarty->tpl_vars[$_key])) {
$_ptr = $this;
// find root
while ($_ptr->parent != null) {
$_ptr = $_ptr->parent;
}
if (isset($_ptr->tpl_vars[$_key])) {
// variable is already defined in root, copy value
$this->smarty->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
$_ptr->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$this->smarty->tpl_vars[$_key] = clone $_value;
$_ptr->tpl_vars[$_key] = clone $_value;
$_ptr->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_GLOBAL_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_GLOBAL_SCOPE) {
if (isset($this->smarty->global_tpl_vars[$_key])) {
// variable is already defined in root, copy value
$this->smarty->global_tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$this->smarty->global_tpl_vars[$_key] = clone $_value;
}
$this->smarty->global_tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
}
}

View File

@@ -193,7 +193,8 @@ class Smarty_Internal_TemplateBase {
{
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];
@@ -205,6 +206,11 @@ class Smarty_Internal_TemplateBase {
$_ptr = null;
}
}
$_ptr = Smarty::instance();
if (isset($_ptr->global_tpl_vars[$variable])) {
// found it, return it
return $_ptr->global_tpl_vars[$variable];
}
if (Smarty::$error_unassigned) {
throw new Exception('Undefined Smarty variable "' . $variable . '"');
} else {

File diff suppressed because it is too large Load Diff

View File

@@ -47,6 +47,11 @@ class Smarty_Method_Get_Template_Vars extends Smarty_Internal_Base {
$_ptr = null;
}
}
if ($search_parents) {
foreach ($this->smarty->global_tpl_vars AS $key => $var) {
$_result[$key] = $var->value;
}
}
return $_result;
}
}