An implementation that makes references to constants in ecshop templates

  • 2020-05-07 19:23:00
  • OfStack

For example, $smarty.const.' constant ', this won't work.
In fact, templating engine is not complicated in principle, just replace some template tags with functions, variables and syntax structure in php.
This time, to add the ability to reference constants in the ecshop template, just add two lines of code to the function make_var()
 
function make_var($val) 
{ 
if (strrpos($val, '.') === false) 
{ 
if (isset($this->_var[$val]) && isset($this->_patchstack[$val])) 
{ 
$val = $this->_patchstack[$val]; 
} 
$p = '$this->_var[\'' . $val . '\']'; 
} 
else 
{ 
$t = explode('.', $val); 
$_var_name = array_shift($t); 
if (isset($this->_var[$_var_name]) && isset($this->_patchstack[$_var_name])) 
{ 
$_var_name = $this->_patchstack[$_var_name]; 
} 
if ($_var_name == 'smarty') 
{ 
if($t[0] == 'const'){ 
return strtoupper($t[1]); 
} 
$p = $this->_compile_smarty_ref($t); 
} 
else 
{ 
$p = '$this->_var[\'' . $_var_name . '\']'; 
} 
foreach ($t AS $val) 
{ 
$p.= '[\'' . $val . '\']'; 
} 
} 
return $p; 
} 

Lines 21-23 are new, which makes it possible to refer to constants defined in php in the template file by {$smarty.const.constant}
 
21 if($t[0] == 'const'){ 
22 return strtoupper($t[1]); 
23 } 

Related articles: