简体   繁体   English

Smarty::$_tpl_vars - 未定义属性 - Smarty 3 中支持 _tpl_vars

[英]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.我正在将旧的 PHP 5.2.14 和 Smarty 2.6.19 迁移到 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. PHP 代码库非常庞大,我使用 Smarty::_tpl_vars 数组来访问分配的变量。

$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此方法似乎已被弃用并替换为getTemplateVars

Is there any workaround for having _tpl_vars in the newer version of Smarty在较新版本的 Smarty 中使用_tpl_vars是否有任何解决方法

You could create a polyfill class that wraps the getTemplateVars method by implementing the ArrayAccess interface and assign it to the Smarty instance as _tpl_vars .您可以创建一个 polyfill class,它通过实现ArrayAccess接口来包装getTemplateVars方法,并将其作为_tpl_vars分配给 Smarty 实例。 You would want to test this very carefully in your application.您可能希望在您的应用程序中对此进行非常仔细的测试。 This is a proof of concept that I just whipped out and tested for this answer to demonstrate that it's possible.这是一个概念证明,我刚刚拿出并测试了这个答案,以证明它是可能的。

In particular you should check to see if the Smarty instance needs to be passed into the constructor by reference in your PHP 5.2 code - I only have 7.4 handy to test with right now.特别是,您应该检查是否需要在 PHP 5.2 代码中通过引用将 Smarty 实例传递到构造函数中——我现在只有 7.4 可以方便地进行测试。

<?php
class TplVarsPolyfill implements ArrayAccess
{
    private $_smarty;

    public function __construct(Smarty $smarty)
    {
        $this->_smarty  = $smarty;
    }

    public function offsetSet($varName, $value)
    {
        $this->_smarty->assign($varName, $value);
    }

    public function offsetExists($offset)
    {
        $tplVars = $this->_smarty->getTemplateVars();
        return isset($tplVars[$offset]);
    }

    public function offsetUnset($varName)
    {
        $this->_smarty->clearAssign($varName);
    }

    public function offsetGet($offset)
    {
        $tplVars = $this->_smarty->getTemplateVars();
        return isset($tplVars[$offset]) ? $tplVars[$offset] : null;
    }

    /**
     * This is required to convince Smarty to set the instance
     * @see Smarty::__set
     * @return mixed
     */ 
    public function _tpl_vars()
    {
        return $this->_smarty->getTemplateVars();
    }
}

$smarty = new Smarty();
$smarty->_tpl_vars = new TplVarsPolyfill($smarty);

$testVarName = 'foo';
$testVal = 'bar';

$smarty->assign($testVarName, $testVal);

assert($smarty->_tpl_vars[$testVarName]==$testVal, 'Value in polyfill should match what was assigned via assign()');

$polyfillVarName = 'animal';
$polyfillVal = 'wombat';

$smarty->_tpl_vars[$polyfillVarName] = $polyfillVal;

$smartyTemplateVars = $smarty->getTemplateVars();
assert($smartyTemplateVars[$polyfillVarName]==$polyfillVal, 'Value in getTemplateVars() should match what was assigned via polyfill');

Managed a workaround by adding the following snippet in Smarty.class.php of PHP 5.2/Smarty 2通过在 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.我会将所有$smarty->_tpl_vars[]替换为 $smarty->getTemplateVars() ,这样可以确保向后兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM