mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 10:54:27 +02:00
Made variable scoping more sensible
This commit is contained in:
@@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
The current date and time is {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
|
The current date and time is {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
|
||||||
|
|
||||||
The value of global assigned variable $SCRIPT_NAME is {$SCRIPT_NAME}
|
|
||||||
|
|
||||||
Example of accessing server environment variable SERVER_NAME: {$smarty.server.SERVER_NAME}
|
Example of accessing server environment variable SERVER_NAME: {$smarty.server.SERVER_NAME}
|
||||||
|
|
||||||
The value of {ldelim}$Name{rdelim} is <b>{$Name}</b>
|
The value of {ldelim}$Name{rdelim} is <b>{$Name}</b>
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
var string Yes *n/a* The name of the variable being assigned
|
var string Yes *n/a* The name of the variable being assigned
|
||||||
value string Yes *n/a* The value being assigned
|
value string Yes *n/a* The value being assigned
|
||||||
index string No *n/a* The index for the new array element. If not specified the value is append to the end of the array.
|
index string No *n/a* The index for the new array element. If not specified the value is append to the end of the array.
|
||||||
scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'global\'
|
scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'smarty\'
|
||||||
|
|
||||||
**Option Flags:**
|
**Option Flags:**
|
||||||
|
|
||||||
|
@@ -21,7 +21,7 @@ execution of a template**.
|
|||||||
---------------- -------- ---------- --------- -----------------------------------------------------------------------
|
---------------- -------- ---------- --------- -----------------------------------------------------------------------
|
||||||
var string Yes *n/a* The name of the variable being assigned
|
var string Yes *n/a* The name of the variable being assigned
|
||||||
value string Yes *n/a* The value being assigned
|
value string Yes *n/a* The value being assigned
|
||||||
scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'global\'
|
scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'smarty\'
|
||||||
|
|
||||||
**Option Flags:**
|
**Option Flags:**
|
||||||
|
|
||||||
|
@@ -238,9 +238,13 @@ abstract class Base implements CompilerInterface {
|
|||||||
protected function convertScope($_attr, $invalidScopes = []): int {
|
protected function convertScope($_attr, $invalidScopes = []): int {
|
||||||
|
|
||||||
static $scopes = [
|
static $scopes = [
|
||||||
'local' => Data::SCOPE_LOCAL, 'parent' => Data::SCOPE_PARENT,
|
'local' => Data::SCOPE_LOCAL, // current scope
|
||||||
'root' => Data::SCOPE_ROOT, 'global' => Data::SCOPE_GLOBAL,
|
'parent' => Data::SCOPE_PARENT, // parent scope (definition unclear)
|
||||||
'tpl_root' => Data::SCOPE_TPL_ROOT, 'smarty' => Data::SCOPE_SMARTY
|
'tpl_root' => Data::SCOPE_TPL_ROOT, // highest template (keep going up until parent is not a template)
|
||||||
|
'root' => Data::SCOPE_ROOT, // highest scope (definition unclear)
|
||||||
|
'global' => Data::SCOPE_GLOBAL, // smarty object
|
||||||
|
|
||||||
|
'smarty' => Data::SCOPE_SMARTY, // @deprecated alias of 'global'
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!isset($_attr['scope'])) {
|
if (!isset($_attr['scope'])) {
|
||||||
|
@@ -78,6 +78,6 @@ class ConfigLoad extends Base {
|
|||||||
$_scope = $this->convertScope($_attr);
|
$_scope = $this->convertScope($_attr);
|
||||||
}
|
}
|
||||||
// create config object
|
// create config object
|
||||||
return "<?php\n\$_smarty_tpl->_loadConfigfile({$conf_file}, {$section});\n?>\n";
|
return "<?php\n\$_smarty_tpl->configLoad({$conf_file}, {$section});\n?>\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
56
src/Data.php
56
src/Data.php
@@ -72,6 +72,12 @@ abstract class Data
|
|||||||
$this->_getSmartyObj()->assign($tpl_var, $value);
|
$this->_getSmartyObj()->assign($tpl_var, $value);
|
||||||
break;
|
break;
|
||||||
case self::SCOPE_TPL_ROOT:
|
case self::SCOPE_TPL_ROOT:
|
||||||
|
$ptr = $this;
|
||||||
|
while (isset($ptr->parent) && ($ptr->parent instanceof Template)) {
|
||||||
|
$ptr = $ptr->parent;
|
||||||
|
}
|
||||||
|
$ptr->assign($tpl_var, $value);
|
||||||
|
break;
|
||||||
case self::SCOPE_ROOT:
|
case self::SCOPE_ROOT:
|
||||||
$ptr = $this;
|
$ptr = $this;
|
||||||
while (isset($ptr->parent) && !($ptr->parent instanceof Smarty)) {
|
while (isset($ptr->parent) && !($ptr->parent instanceof Smarty)) {
|
||||||
@@ -82,6 +88,9 @@ abstract class Data
|
|||||||
case self::SCOPE_PARENT:
|
case self::SCOPE_PARENT:
|
||||||
if ($this->parent) {
|
if ($this->parent) {
|
||||||
$this->parent->assign($tpl_var, $value);
|
$this->parent->assign($tpl_var, $value);
|
||||||
|
} else {
|
||||||
|
// assign local as fallback
|
||||||
|
$this->assign($tpl_var, $value);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -376,21 +385,48 @@ abstract class Data
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a single or all global variables
|
* load a config file, optionally load just selected sections
|
||||||
*
|
*
|
||||||
* @api Smarty::getGlobal()
|
* @param string $config_file filename
|
||||||
|
* @param mixed $sections array of section names, single
|
||||||
|
* section or null
|
||||||
*
|
*
|
||||||
* @param string $varName variable name or null
|
* @return $this
|
||||||
|
* @throws \Exception
|
||||||
|
*@api Smarty::configLoad()
|
||||||
|
* @link https://www.smarty.net/docs/en/api.config.load.tpl
|
||||||
*
|
*
|
||||||
* @return string|array variable value or or array of variables
|
|
||||||
*
|
|
||||||
* @deprecated since 5.0
|
|
||||||
*/
|
*/
|
||||||
public function getGlobal($varName = null)
|
public function configLoad($config_file, $sections = null)
|
||||||
{
|
{
|
||||||
trigger_error(__METHOD__ . " is deprecated. Use \\Smarty\\Smarty::getValue() to retrieve a variable " .
|
$this->_loadConfigfile($config_file, $sections);
|
||||||
" at the Smarty level.", E_USER_DEPRECATED);
|
return $this;
|
||||||
return $this->_getSmartyObj()->getValue($varName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load a config file, optionally load just selected sections
|
||||||
|
*
|
||||||
|
* @param string $config_file filename
|
||||||
|
* @param mixed $sections array of section names, single
|
||||||
|
* section or null
|
||||||
|
|
||||||
|
* @returns Template
|
||||||
|
* @throws \Exception
|
||||||
|
* @link https://www.smarty.net/docs/en/api.config.load.tpl
|
||||||
|
*
|
||||||
|
* @api Smarty::configLoad()
|
||||||
|
*/
|
||||||
|
protected function _loadConfigfile($config_file, $sections = null)
|
||||||
|
{
|
||||||
|
$smarty = $this->_getSmartyObj();
|
||||||
|
|
||||||
|
$confObj = new Template($config_file, $smarty, $this, null, null, null, null, true);
|
||||||
|
$confObj->caching = Smarty::CACHING_OFF;
|
||||||
|
$confObj->source->config_sections = $sections;
|
||||||
|
$confObj->compiled = \Smarty\Template\Compiled::load($confObj);
|
||||||
|
$confObj->compiled->render($confObj);
|
||||||
|
return $confObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -292,27 +292,6 @@ class Debug extends Data
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$config_vars = array_merge($parent->config_vars, $config_vars);
|
$config_vars = array_merge($parent->config_vars, $config_vars);
|
||||||
} else {
|
|
||||||
foreach ($this->_getSmartyObj()->getAllGlobalTemplateVars() as $key => $var) {
|
|
||||||
if (!array_key_exists($key, $tpl_vars)) {
|
|
||||||
foreach ($var as $varkey => $varvalue) {
|
|
||||||
if ($varkey === 'value') {
|
|
||||||
$tpl_vars[ $key ][ $varkey ] = $varvalue;
|
|
||||||
} else {
|
|
||||||
if ($varkey === 'nocache') {
|
|
||||||
if ($varvalue === true) {
|
|
||||||
$tpl_vars[ $key ][ $varkey ] = $varvalue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($varkey !== 'scope' || $varvalue !== 0) {
|
|
||||||
$tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$tpl_vars[ $key ][ 'scope' ] = 'Global';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (object)array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
|
return (object)array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
|
||||||
}
|
}
|
||||||
|
@@ -164,7 +164,7 @@ class InheritanceRuntime {
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function process(
|
private function process(
|
||||||
Template $tpl,
|
Template $tpl,
|
||||||
\Smarty\Runtime\Block $block,
|
\Smarty\Runtime\Block $block,
|
||||||
\Smarty\Runtime\Block $parent = null
|
\Smarty\Runtime\Block $parent = null
|
||||||
|
@@ -972,23 +972,23 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
* @param mixed $cache_id cache id to be used with this template
|
* @param mixed $cache_id cache id to be used with this template
|
||||||
* @param mixed $compile_id compile id to be used with this template
|
* @param mixed $compile_id compile id to be used with this template
|
||||||
* @param object $parent next higher level of Smarty variables
|
* @param object $parent next higher level of Smarty variables
|
||||||
* @param boolean $do_clone flag is template object shall be cloned
|
|
||||||
*
|
*
|
||||||
* @return \Smarty\Template template object
|
* @return \Smarty\Template template object
|
||||||
* @throws \Smarty\Exception
|
* @throws \Smarty\Exception
|
||||||
*/
|
*/
|
||||||
public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
|
public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
|
||||||
{
|
{
|
||||||
if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) {
|
if ((is_object($cache_id) || is_array($cache_id))) {
|
||||||
$parent = $cache_id;
|
$parent = $cache_id;
|
||||||
$cache_id = null;
|
$cache_id = null;
|
||||||
}
|
}
|
||||||
if ($parent !== null && is_array($parent)) {
|
if (is_array($parent)) {
|
||||||
$data = $parent;
|
$data = $parent;
|
||||||
$parent = null;
|
$parent = null;
|
||||||
} else {
|
} else {
|
||||||
$data = null;
|
$data = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->_templateDirNormalized) {
|
if (!$this->_templateDirNormalized) {
|
||||||
$this->_normalizeTemplateConfig(false);
|
$this->_normalizeTemplateConfig(false);
|
||||||
}
|
}
|
||||||
@@ -1005,6 +1005,9 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
$tpl->assign($_key, $_val);
|
$tpl->assign($_key, $_val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$tpl->tplFunctions = array_merge($parent->tplFunctions ?? [], $tpl->tplFunctions ?? []);
|
||||||
|
|
||||||
if (!$this->debugging && $this->debugging_ctrl === 'URL') {
|
if (!$this->debugging && $this->debugging_ctrl === 'URL') {
|
||||||
$tpl->smarty->getDebug()->debugUrl($tpl->smarty);
|
$tpl->smarty->getDebug()->debugUrl($tpl->smarty);
|
||||||
}
|
}
|
||||||
@@ -2196,51 +2199,19 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
$this->cacheResource = $cacheResource;
|
$this->cacheResource = $cacheResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a global variable, available in all templates
|
|
||||||
*
|
|
||||||
* @param string $varName
|
|
||||||
* @param Variable $param
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setGlobalVariable(string $varName, Variable $param) {
|
|
||||||
$this->global_tpl_vars[$varName] = $param;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all global variables
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAllGlobalTemplateVars() {
|
|
||||||
return $this->global_tpl_vars;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a single global variable, or null if not found.
|
|
||||||
* @param string $varName
|
|
||||||
*
|
|
||||||
* @return Variable|null
|
|
||||||
*/
|
|
||||||
public function getGlobalVariable(string $varName): ?Variable {
|
|
||||||
return $this->global_tpl_vars[$varName] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetches a rendered Smarty template
|
* fetches a rendered Smarty template
|
||||||
*
|
*
|
||||||
* @param string $template the resource handle of the template file or template object
|
* @param string $template the resource handle of the template file or template object
|
||||||
* @param mixed $cache_id cache id to be used with this template
|
* @param mixed $cache_id cache id to be used with this template
|
||||||
* @param mixed $compile_id compile id to be used with this template
|
* @param mixed $compile_id compile id to be used with this template
|
||||||
* @param object $parent next higher level of Smarty variables
|
|
||||||
*
|
*
|
||||||
* @return string rendered template output
|
* @return string rendered template output
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null) {
|
public function fetch($template = null, $cache_id = null, $compile_id = null) {
|
||||||
return $this->returnOrCreateTemplate($template)->fetch($cache_id, $compile_id, $parent);
|
return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2249,13 +2220,12 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
* @param string $template the resource handle of the template file or template object
|
* @param string $template the resource handle of the template file or template object
|
||||||
* @param mixed $cache_id cache id to be used with this template
|
* @param mixed $cache_id cache id to be used with this template
|
||||||
* @param mixed $compile_id compile id to be used with this template
|
* @param mixed $compile_id compile id to be used with this template
|
||||||
* @param object $parent next higher level of Smarty variables
|
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @throws \Smarty\Exception
|
* @throws \Smarty\Exception
|
||||||
*/
|
*/
|
||||||
public function display($template = null, $cache_id = null, $compile_id = null, $parent = null) {
|
public function display($template = null, $cache_id = null, $compile_id = null) {
|
||||||
return $this->returnOrCreateTemplate($template)->display($cache_id, $compile_id, $parent);
|
return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2265,7 +2235,6 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
* object
|
* object
|
||||||
* @param mixed $cache_id cache id to be used with this template
|
* @param mixed $cache_id cache id to be used with this template
|
||||||
* @param mixed $compile_id compile id to be used with this template
|
* @param mixed $compile_id compile id to be used with this template
|
||||||
* @param object $parent next higher level of Smarty variables
|
|
||||||
*
|
*
|
||||||
* @return bool cache status
|
* @return bool cache status
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
@@ -2274,8 +2243,8 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
*
|
*
|
||||||
* @api Smarty::isCached()
|
* @api Smarty::isCached()
|
||||||
*/
|
*/
|
||||||
public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null) {
|
public function isCached($template = null, $cache_id = null, $compile_id = null) {
|
||||||
return $this->returnOrCreateTemplate($template)->isCached($cache_id, $compile_id, $parent);
|
return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->isCached();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2287,9 +2256,9 @@ class Smarty extends \Smarty\TemplateBase
|
|||||||
* @return Template
|
* @return Template
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function returnOrCreateTemplate($template, $cache_id = null, $compile_id = null, $parent = null) {
|
private function returnOrCreateTemplate($template, $cache_id = null, $compile_id = null) {
|
||||||
if (!($template instanceof Template)) {
|
if (!($template instanceof Template)) {
|
||||||
$template = $this->createTemplate($template, $cache_id, $compile_id, $parent ?: $this, false);
|
$template = $this->createTemplate($template, $cache_id, $compile_id, $this);
|
||||||
$template->caching = $this->caching;
|
$template->caching = $this->caching;
|
||||||
}
|
}
|
||||||
return $template;
|
return $template;
|
||||||
|
@@ -693,7 +693,7 @@ class Template extends TemplateBase {
|
|||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
public function _loadConfigfile($config_file, $sections = null)
|
protected function _loadConfigfile($config_file, $sections = null)
|
||||||
{
|
{
|
||||||
$confObj = parent::_loadConfigfile($config_file, $sections);
|
$confObj = parent::_loadConfigfile($config_file, $sections);
|
||||||
|
|
||||||
@@ -703,13 +703,13 @@ class Template extends TemplateBase {
|
|||||||
return $confObj;
|
return $confObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch($cache_id = null, $compile_id = null, $parent = null) {
|
public function fetch() {
|
||||||
$result = $this->_execute($cache_id, $compile_id, $parent, 0);
|
$result = $this->_execute(0);
|
||||||
return $result === null ? ob_get_clean() : $result;
|
return $result === null ? ob_get_clean() : $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display($cache_id = null, $compile_id = null, $parent = null) {
|
public function display() {
|
||||||
$this->_execute($cache_id, $compile_id, $parent, 1);
|
$this->_execute(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -726,23 +726,20 @@ class Template extends TemplateBase {
|
|||||||
*
|
*
|
||||||
* @api Smarty::isCached()
|
* @api Smarty::isCached()
|
||||||
*/
|
*/
|
||||||
public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null): bool {
|
public function isCached(): bool {
|
||||||
return (bool) $this->_execute($template, $cache_id, $compile_id, $parent, 2);
|
return (bool) $this->_execute(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetches a rendered Smarty template
|
* fetches a rendered Smarty template
|
||||||
*
|
*
|
||||||
* @param mixed $cache_id cache id to be used with this template
|
|
||||||
* @param mixed $compile_id compile id to be used with this template
|
|
||||||
* @param object $parent next higher level of Smarty variables
|
|
||||||
* @param string $function function type 0 = fetch, 1 = display, 2 = isCache
|
* @param string $function function type 0 = fetch, 1 = display, 2 = isCache
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
* @throws \Smarty\Exception|\Throwable
|
* @throws \Throwable
|
||||||
*/
|
*/
|
||||||
private function _execute($cache_id, $compile_id, $parent, $function) {
|
private function _execute($function) {
|
||||||
|
|
||||||
$smarty = $this->_getSmartyObj();
|
$smarty = $this->_getSmartyObj();
|
||||||
|
|
||||||
@@ -759,8 +756,6 @@ class Template extends TemplateBase {
|
|||||||
$errorHandler->activate();
|
$errorHandler->activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->tplFunctions = array_merge($parent->tplFunctions ?? [], $this->tplFunctions ?? []);
|
|
||||||
|
|
||||||
if ($function === 2) {
|
if ($function === 2) {
|
||||||
if ($this->caching) {
|
if ($this->caching) {
|
||||||
// return cache status of template
|
// return cache status of template
|
||||||
|
@@ -218,7 +218,7 @@ class Cached extends ResourceBase {
|
|||||||
* @param Template $_template template object
|
* @param Template $_template template object
|
||||||
* @param bool $update flag if called because cache update
|
* @param bool $update flag if called because cache update
|
||||||
*/
|
*/
|
||||||
public function process(Template $_template, $update = false) {
|
private function process(Template $_template, $update = false) {
|
||||||
if ($this->handler->process($_template, $this, $update) === false) {
|
if ($this->handler->process($_template, $this, $update) === false) {
|
||||||
$this->valid = false;
|
$this->valid = false;
|
||||||
}
|
}
|
||||||
@@ -281,7 +281,7 @@ class Cached extends ResourceBase {
|
|||||||
*
|
*
|
||||||
* @throws \Smarty\Exception
|
* @throws \Smarty\Exception
|
||||||
*/
|
*/
|
||||||
public function updateCache(Template $_template, $no_output_filter) {
|
private function updateCache(Template $_template, $no_output_filter) {
|
||||||
ob_start();
|
ob_start();
|
||||||
if (!isset($_template->compiled)) {
|
if (!isset($_template->compiled)) {
|
||||||
$_template->loadCompiled();
|
$_template->loadCompiled();
|
||||||
|
@@ -125,7 +125,7 @@ class Compiled extends ResourceBase {
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function process(Template $_smarty_tpl) {
|
private function process(Template $_smarty_tpl) {
|
||||||
$source = &$_smarty_tpl->source;
|
$source = &$_smarty_tpl->source;
|
||||||
$smarty = &$_smarty_tpl->smarty;
|
$smarty = &$_smarty_tpl->smarty;
|
||||||
if ($source->handler->recompiled) {
|
if ($source->handler->recompiled) {
|
||||||
|
@@ -92,13 +92,6 @@ abstract class ResourceBase {
|
|||||||
*/
|
*/
|
||||||
public $isCache = false;
|
public $isCache = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* Process resource
|
|
||||||
*
|
|
||||||
* @param Template $_template template object
|
|
||||||
*/
|
|
||||||
abstract public function process(Template $_template);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get rendered template content by calling compiled or cached template code
|
* get rendered template content by calling compiled or cached template code
|
||||||
*
|
*
|
||||||
|
@@ -434,48 +434,6 @@ abstract class TemplateBase extends Data {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* load a config file, optionally load just selected sections
|
|
||||||
*
|
|
||||||
* @param string $config_file filename
|
|
||||||
* @param mixed $sections array of section names, single
|
|
||||||
* section or null
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @throws \Exception
|
|
||||||
*@api Smarty::configLoad()
|
|
||||||
* @link https://www.smarty.net/docs/en/api.config.load.tpl
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function configLoad($config_file, $sections = null)
|
|
||||||
{
|
|
||||||
$this->_loadConfigfile($config_file, $sections);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* load a config file, optionally load just selected sections
|
|
||||||
*
|
|
||||||
* @param string $config_file filename
|
|
||||||
* @param mixed $sections array of section names, single
|
|
||||||
* section or null
|
|
||||||
|
|
||||||
* @returns Template
|
|
||||||
* @throws \Exception
|
|
||||||
* @link https://www.smarty.net/docs/en/api.config.load.tpl
|
|
||||||
*
|
|
||||||
* @api Smarty::configLoad()
|
|
||||||
*/
|
|
||||||
public function _loadConfigfile($config_file, $sections = null)
|
|
||||||
{
|
|
||||||
$smarty = $this->_getSmartyObj();
|
|
||||||
|
|
||||||
$confObj = new Template($config_file, $smarty, $this, null, null, null, null, true);
|
|
||||||
$confObj->caching = Smarty::CACHING_OFF;
|
|
||||||
$confObj->source->config_sections = $sections;
|
|
||||||
$confObj->compiled = \Smarty\Template\Compiled::load($confObj);
|
|
||||||
$confObj->compiled->render($confObj);
|
|
||||||
return $confObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -181,7 +181,8 @@ class ConfigVarTest extends PHPUnit_Smarty
|
|||||||
{
|
{
|
||||||
$data = $this->smarty->createData();
|
$data = $this->smarty->createData();
|
||||||
$data->configLoad('test.conf');
|
$data->configLoad('test.conf');
|
||||||
$this->assertEquals("123bvc", $this->smarty->fetch('text.tpl', $data));
|
$tpl = $this->smarty->createTemplate('text.tpl', $data);
|
||||||
|
$this->assertEquals("123bvc", $this->smarty->fetch($tpl));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,69 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Smarty PHPunit tests assignGlobal method and {assignGlobal} tag
|
|
||||||
*
|
|
||||||
|
|
||||||
* @author Uwe Tews
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class for assignGlobal method and {assignGlobal} tag tests
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class AssignGlobalTest extends PHPUnit_Smarty
|
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
$this->setUpSmarty(__DIR__);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function testInit()
|
|
||||||
{
|
|
||||||
$this->cleanDirs();
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* test assignGlobal and getGlobal
|
|
||||||
*/
|
|
||||||
public function testAssignGlobalGetGlobal()
|
|
||||||
{
|
|
||||||
$this->smarty->assignGlobal('foo', 'bar');
|
|
||||||
$this->assertEquals('bar', $this->smarty->getGlobal('foo'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test assignGlobal and getGlobal on arrays
|
|
||||||
*/
|
|
||||||
public function testAssignGlobalGetGlobalArray()
|
|
||||||
{
|
|
||||||
$this->smarty->assignGlobal('foo', array('foo' => 'bar', 'foo2' => 'bar2'));
|
|
||||||
$a1 = array('foo' => array('foo' => 'bar', 'foo2' => 'bar2'));
|
|
||||||
$a2 = $this->smarty->getGlobal();
|
|
||||||
$this->assertTrue($a1 === $a2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test assignGlobal tag
|
|
||||||
*/
|
|
||||||
public function testAssignGlobalTag()
|
|
||||||
{
|
|
||||||
$this->smarty->assignGlobal('foo', 'bar');
|
|
||||||
$this->assertEquals('bar', $this->smarty->fetch('eval:{$foo}'));
|
|
||||||
$this->assertEquals('buh', $this->smarty->fetch('eval:{assign var=foo value=buh scope=global}{$foo}'));
|
|
||||||
$this->assertEquals('buh', $this->smarty->fetch('eval:{$foo}'));
|
|
||||||
$this->assertEquals('buh', $this->smarty->getGlobal('foo'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test global var array element tag
|
|
||||||
*/
|
|
||||||
public function testGlobalVarArrayTag()
|
|
||||||
{
|
|
||||||
$this->smarty->assignGlobal('foo', array('foo' => 'bar', 'foo2' => 'bar2'));
|
|
||||||
$this->assertEquals('bar2', $this->smarty->fetch('eval:{$foo.foo2}'));
|
|
||||||
$this->assertEquals('bar', $this->smarty->fetch('eval:{$foo.foo}'));
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,2 +0,0 @@
|
|||||||
# Ignore anything in here, but keep this directory
|
|
||||||
*
|
|
@@ -1,2 +0,0 @@
|
|||||||
# Ignore anything in here, but keep this directory
|
|
||||||
*
|
|
@@ -37,15 +37,16 @@ class ScopeTest extends PHPUnit_Smarty
|
|||||||
{
|
{
|
||||||
$file = "testAppendScope_{$testNumber}.tpl";
|
$file = "testAppendScope_{$testNumber}.tpl";
|
||||||
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
|
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
|
||||||
$this->smarty->assignGlobal('file', $file);
|
$this->smarty->assign('file', $file);
|
||||||
$this->smarty->assign('foo', 'smarty');
|
$this->smarty->assign('foo', 'global');
|
||||||
$this->smarty->assignGlobal('foo', 'global');
|
|
||||||
$data1 = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
$data1 = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
||||||
$data = $this->smarty->createData($data1);
|
$data = $this->smarty->createData($data1);
|
||||||
$data1->assign('foo', 'data1');
|
$data1->assign('foo', 'data1');
|
||||||
$data->assign('foo', 'data');
|
$data->assign('foo', 'data');
|
||||||
$this->assertEquals($result, $this->smarty->fetch('scope_tag.tpl', $data),
|
|
||||||
"test - {$code} - {$testName}");
|
$tpl = $this->smarty->createTemplate('scope_tag.tpl', $data);
|
||||||
|
|
||||||
|
$this->assertEquals($result, $this->smarty->fetch($tpl),"test - {$code} - {$testName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,18 +61,53 @@ class ScopeTest extends PHPUnit_Smarty
|
|||||||
* result
|
* result
|
||||||
* test name
|
* test name
|
||||||
*/
|
*/
|
||||||
return array(array('{$foo[] = \'newvar\' scope=tpl_root}', true,
|
return [
|
||||||
'#testAppendScope_0.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =\'global\'',
|
[
|
||||||
'', $i ++,),
|
'{$foo[] = \'newvar\' scope=tpl_root}',
|
||||||
array('{append var=foo value=\'newvar\' scope=tpl_root}', true,
|
true,
|
||||||
'#testAppendScope_1.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =\'global\'',
|
'#testAppendScope_0.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
'', $i ++,),
|
'#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
array('{append var=foo value=\'newvar\' scope=global}', true,
|
'#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
'#testAppendScope_2.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =array(0=>\'data\',1=>\'newvar\',)',
|
'#data:$foo =\'data\'' .
|
||||||
'', $i ++,),
|
'#data:$foo =\'data1\'' .
|
||||||
array('{append var=foo value=\'newvar\' scope=smarty}', true,
|
'#global:$foo =\'global\'',
|
||||||
'#testAppendScope_3.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =array(0=>\'data\',1=>\'newvar\',)#global:$foo =\'global\'',
|
'',
|
||||||
'', $i ++,),);
|
$i++,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'{append var=foo value=\'newvar\' scope=tpl_root}', true,
|
||||||
|
'#testAppendScope_1.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
|
'#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
|
'#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' .
|
||||||
|
'#data:$foo =\'data\'' .
|
||||||
|
'#data:$foo =\'data1\'' .
|
||||||
|
'#global:$foo =\'global\'',
|
||||||
|
'',
|
||||||
|
$i++,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'{append var=foo value=\'newvar\' scope=global}', true,
|
||||||
|
'#testAppendScope_2.tpl:$foo =\'data\'' .
|
||||||
|
'#scope_include.tpl:$foo =\'data\'' .
|
||||||
|
'#scope_tag.tpl:$foo =\'data\'' .
|
||||||
|
'#data:$foo =\'data\'' .
|
||||||
|
'#data:$foo =\'data1\'' .
|
||||||
|
'#global:$foo =array(0=>\'data\',1=>\'newvar\',)',
|
||||||
|
'',
|
||||||
|
$i++,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'{append var=foo value=\'newvar\' scope=smarty}', true,
|
||||||
|
'#testAppendScope_3.tpl:$foo =\'data\'' .
|
||||||
|
'#scope_include.tpl:$foo =\'data\'' .
|
||||||
|
'#scope_tag.tpl:$foo =\'data\'' .
|
||||||
|
'#data:$foo =\'data\'' .
|
||||||
|
'#data:$foo =\'data1\'' .
|
||||||
|
'#global:$foo =array(0=>\'data\',1=>\'newvar\',)',
|
||||||
|
'',
|
||||||
|
$i++,
|
||||||
|
],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,8 +123,10 @@ class ScopeTest extends PHPUnit_Smarty
|
|||||||
$this->smarty->assignGlobal('foo', 'global');
|
$this->smarty->assignGlobal('foo', 'global');
|
||||||
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
||||||
$data->assign('foo', 'data');
|
$data->assign('foo', 'data');
|
||||||
$this->assertEquals('#' . $file . $result,
|
|
||||||
$this->smarty->fetch('scope_tag.tpl', $data), "test - {$code} - {$testName}");
|
$tpl = $this->smarty->createTemplate('scope_tag.tpl', $data);
|
||||||
|
|
||||||
|
$this->assertEquals('#' . $file . $result, $this->smarty->fetch($tpl), "test - {$code} - {$testName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -186,8 +224,8 @@ class ScopeTest extends PHPUnit_Smarty
|
|||||||
if (!$useSmarty) {
|
if (!$useSmarty) {
|
||||||
$testName .= 'no smarty';
|
$testName .= 'no smarty';
|
||||||
}
|
}
|
||||||
$this->assertEquals($result, $this->smarty->fetch('test_scope.tpl', $data),
|
$tpl = $this->smarty->createTemplate('test_scope.tpl', $data);
|
||||||
"test - {$code} - {$testName}");
|
$this->assertEquals($result, $this->smarty->fetch($tpl), "test - {$code} - {$testName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -251,8 +289,12 @@ class ScopeTest extends PHPUnit_Smarty
|
|||||||
$this->smarty->configLoad('smarty.conf');
|
$this->smarty->configLoad('smarty.conf');
|
||||||
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
|
||||||
$data->configLoad('data.conf');
|
$data->configLoad('data.conf');
|
||||||
$this->assertEquals('#' . $file . $result,
|
$tpl = $this->smarty->createTemplate('scope_tag.tpl', $data);
|
||||||
$this->smarty->fetch('scope_tag.tpl', $data), "test - {$code} - {$testName}");
|
$this->assertEquals(
|
||||||
|
'#' . $file . $result,
|
||||||
|
$this->smarty->fetch($tpl),
|
||||||
|
"test - {$code} - {$testName}
|
||||||
|
");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@@ -20,7 +20,7 @@ use Smarty\Template;
|
|||||||
function smarty_function_checkvar($params, \Smarty\Template $template)
|
function smarty_function_checkvar($params, \Smarty\Template $template)
|
||||||
{
|
{
|
||||||
$output = '';
|
$output = '';
|
||||||
$types = array('template', 'data', 'smarty', 'global');
|
$types = ['template', 'data', 'global'];
|
||||||
if (isset($params['types'])) {
|
if (isset($params['types'])) {
|
||||||
$types = (array)$params['types'];
|
$types = (array)$params['types'];
|
||||||
}
|
}
|
||||||
@@ -45,15 +45,10 @@ function smarty_function_checkvar($params, \Smarty\Template $template)
|
|||||||
$ptr = null;
|
$ptr = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (in_array('smarty', $types)) {
|
if (in_array('global', $types)) {
|
||||||
$output .= "#Smarty:\${$var} =";
|
$output .= "#global:\${$var} =";
|
||||||
$output .= $template->smarty->hasVariable($var) ?
|
$output .= $template->smarty->hasVariable($var) ?
|
||||||
preg_replace('/\s/', '', var_export($template->smarty->getValue($var), true)) : '>unassigned<';
|
preg_replace('/\s/', '', var_export($template->smarty->getValue($var), true)) : '>unassigned<';
|
||||||
}
|
}
|
||||||
if (in_array('global', $types)) {
|
|
||||||
$output .= "#global:\${$var} =";
|
|
||||||
$output .= $template->_getSmartyObj()->getGlobalVariable($var) ?
|
|
||||||
preg_replace('/\s/', '', var_export($template->_getSmartyObj()->getGlobalVariable($var)->getValue(), true)) : '>unassigned<';
|
|
||||||
}
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user