2009-03-22 16:09:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-07-08 18:30:03 +00:00
|
|
|
* Smarty Internal Plugin Compile Assign
|
|
|
|
* Compiles the {assign} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2010-07-08 18:30:03 +00:00
|
|
|
* @subpackage Compiler
|
2014-06-06 02:40:04 +00:00
|
|
|
* @author Uwe Tews
|
2010-07-08 18:30:03 +00:00
|
|
|
*/
|
2010-08-17 15:39:51 +00:00
|
|
|
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
2010-07-08 18:30:03 +00:00
|
|
|
* Smarty Internal Plugin Compile Assign Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-07-08 18:30:03 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2016-02-09 23:27:07 +01:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2016-03-09 01:01:32 +01:00
|
|
|
public $option_flags = array('nocache', 'noscope');
|
2016-02-09 23:27:07 +01:00
|
|
|
|
2018-06-12 09:58:15 +02:00
|
|
|
/**
|
2015-11-01 02:02:27 +01:00
|
|
|
* Valid scope names
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-08-31 16:45:09 +02:00
|
|
|
public $valid_scopes = array(
|
|
|
|
'local' => Smarty::SCOPE_LOCAL, 'parent' => Smarty::SCOPE_PARENT,
|
|
|
|
'root' => Smarty::SCOPE_ROOT, 'global' => Smarty::SCOPE_GLOBAL,
|
|
|
|
'tpl_root' => Smarty::SCOPE_TPL_ROOT, 'smarty' => Smarty::SCOPE_SMARTY
|
|
|
|
);
|
2015-11-01 02:02:27 +01:00
|
|
|
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
2010-07-08 18:30:03 +00:00
|
|
|
* Compiles code for the {assign} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2018-06-12 09:58:15 +02:00
|
|
|
* @param array $args array with attributes from parser
|
2015-08-06 01:19:11 +02:00
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
2018-06-12 09:58:15 +02:00
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2010-07-08 18:30:03 +00:00
|
|
|
* @return string compiled code
|
2015-08-06 01:19:11 +02:00
|
|
|
* @throws \SmartyCompilerException
|
2010-07-08 18:30:03 +00:00
|
|
|
*/
|
2015-08-06 01:19:11 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-03-22 16:09:05 +00:00
|
|
|
{
|
2010-12-28 22:40:13 +00:00
|
|
|
// the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
|
2009-03-22 16:09:05 +00:00
|
|
|
$this->required_attributes = array('var', 'value');
|
2010-11-11 21:34:36 +00:00
|
|
|
$this->shorttag_order = array('var', 'value');
|
2016-02-09 23:27:07 +01:00
|
|
|
$this->optional_attributes = array('scope');
|
2016-02-14 19:58:55 +01:00
|
|
|
$this->mapCache = array();
|
2016-02-09 23:27:07 +01:00
|
|
|
$_nocache = false;
|
2009-03-22 16:09:05 +00:00
|
|
|
// check and get attributes
|
2011-09-16 14:19:56 +00:00
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
|
|
|
// nocache ?
|
2016-02-09 23:27:07 +01:00
|
|
|
if ($_var = $compiler->getId($_attr[ 'var' ])) {
|
|
|
|
$_var = "'{$_var}'";
|
|
|
|
} else {
|
|
|
|
$_var = $_attr[ 'var' ];
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
if ($compiler->tag_nocache || $compiler->nocache) {
|
2016-02-09 23:27:07 +01:00
|
|
|
$_nocache = true;
|
2010-07-31 13:29:59 +00:00
|
|
|
// create nocache var to make it know for further compiling
|
2016-02-09 23:27:07 +01:00
|
|
|
$compiler->setNocacheInVariable($_attr[ 'var' ]);
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2010-11-11 21:34:36 +00:00
|
|
|
// scope setup
|
2016-03-09 01:01:32 +01:00
|
|
|
if ($_attr[ 'noscope' ]) {
|
2018-08-31 16:45:09 +02:00
|
|
|
$_scope = -1;
|
2016-03-09 01:01:32 +01:00
|
|
|
} else {
|
|
|
|
$_scope = $compiler->convertScope($_attr, $this->valid_scopes);
|
|
|
|
}
|
2016-02-09 23:27:07 +01:00
|
|
|
// optional parameter
|
2017-11-06 01:02:56 +01:00
|
|
|
$_params = '';
|
2016-02-09 23:27:07 +01:00
|
|
|
if ($_nocache || $_scope) {
|
|
|
|
$_params .= ' ,' . var_export($_nocache, true);
|
|
|
|
}
|
|
|
|
if ($_scope) {
|
|
|
|
$_params .= ' ,' . $_scope;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-12-31 02:20:47 +01:00
|
|
|
if (isset($parameter[ 'smarty_internal_index' ])) {
|
|
|
|
$output =
|
2016-02-09 23:27:07 +01:00
|
|
|
"<?php \$_tmp_array = isset(\$_smarty_tpl->tpl_vars[{$_var}]) ? \$_smarty_tpl->tpl_vars[{$_var}]->value : array();\n";
|
2019-05-13 17:15:14 +02:00
|
|
|
$output .= "if (!(is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess)) {\n";
|
2016-02-09 23:27:07 +01:00
|
|
|
$output .= "settype(\$_tmp_array, 'array');\n";
|
|
|
|
$output .= "}\n";
|
|
|
|
$output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
|
2017-11-20 04:07:51 +01:00
|
|
|
$output .= "\$_smarty_tpl->_assignInScope({$_var}, \$_tmp_array{$_params});?>";
|
2016-02-09 23:27:07 +01:00
|
|
|
} else {
|
2017-11-20 04:07:51 +01:00
|
|
|
$output = "<?php \$_smarty_tpl->_assignInScope({$_var}, {$_attr['value']}{$_params});?>";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|