phpsmartysmarty3

Smarty::$_tpl_vars - Undefined property - Support for _tpl_vars in Smarty 3


I am migrating my old PHP 5.2.14 and Smarty 2.6.19 to PHP 7.3.25 + Smarty 3.1.34.

The PHP code base is quite huge and I have used Smarty::_tpl_vars array to access assigned variables.

$smarty->assign("myvar","var-value"); 
$myvar = $smarty->_tpl_vars['myvar']; #this returns "var-value"  

This method seem to have been deprecated and replaced with getTemplateVars

Is there any workaround for having _tpl_vars in the newer version of Smarty


Solution

  • Managed a workaround by adding the following snippet in Smarty.class.php of PHP 5.2/Smarty 2

    function getTemplateVars($name=null)
    {
        return $this->_tpl_vars[$name];
    }
    

    I will replace all $smarty->_tpl_vars[] with $smarty->getTemplateVars() and this way ensure backward compatibility.