Files
smarty/libs/sysplugins/smarty_internal_method_gettemplatevars.php
Simon Wisselink 39b69f0142 Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.

Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions

See the changelog for more details.

Switched CI from Travis to Github CI.
2021-10-13 12:15:17 +02:00

120 lines
3.9 KiB
PHP

<?php
/**
* Smarty Method GetTemplateVars
*
* Smarty::getTemplateVars() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetTemplateVars
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* Returns a single or all template variables
*
* @api Smarty::getTemplateVars()
* @link https://www.smarty.net/docs/en/api.get.template.vars.tpl
*
* @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
* @param string $varName variable name or null
* @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents include parent templates?
*
* @return mixed variable value or or array of variables
*/
public function getTemplateVars(
Smarty_Internal_Data $data,
$varName = null,
Smarty_Internal_Data $_ptr = null,
$searchParents = true
) {
if (isset($varName)) {
$_var = $this->_getVariable($data, $varName, $_ptr, $searchParents, false);
if (is_object($_var)) {
return $_var->value;
} else {
return null;
}
} else {
$_result = array();
if ($_ptr === null) {
$_ptr = $data;
}
while ($_ptr !== null) {
foreach ($_ptr->tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if ($searchParents && isset(Smarty::$global_tpl_vars)) {
foreach (Smarty::$global_tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
}
return $_result;
}
}
/**
* gets the object of a Smarty variable
*
* @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
* @param string $varName the name of the Smarty variable
* @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents search also in parent data
* @param bool $errorEnable
*
* @return \Smarty_Variable
*/
public function _getVariable(
Smarty_Internal_Data $data,
$varName,
Smarty_Internal_Data $_ptr = null,
$searchParents = true,
$errorEnable = true
) {
if ($_ptr === null) {
$_ptr = $data;
}
while ($_ptr !== null) {
if (isset($_ptr->tpl_vars[ $varName ])) {
// found it, return it
return $_ptr->tpl_vars[ $varName ];
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if (isset(Smarty::$global_tpl_vars[ $varName ])) {
// found it, return it
return Smarty::$global_tpl_vars[ $varName ];
}
if ($errorEnable && $data->_getSmartyObj()->error_unassigned) {
// force a notice
$x = $$varName;
}
return new Smarty_Undefined_Variable;
}
}