将php变量传递给Twig - 文档抛出错误

问题描述 投票:0回答:1

更新:1

我在DarkBee的帮助下使用这种方法

$detect = new Mobile_Detect;
class Project_Twig_Extensions extends Twig_Extension{
    public function getFunctions(){
        return array(new Twig_SimpleFunction('detectDevice', function() use($detect){
            if($detect->isMobile()){
                return true;
            } else {
                return false;
            }
        }));
    }
}

我在Twig中使用它来调用它

{% if detectDevice %}

没有值传递并且条件没有得到满足所以检查文档我写了另一种方法以防万一https://twig.symfony.com/doc/2.x/advanced.html#functions

$twig = new Twig_environment($loader);
$function = new Twig_Function('detectDevice', function() use($detect){
    if($detect->isMobile()){
         return true;
    } else {
         return false;
    }
});
$twig->addFunction($function);

这也没有传递任何值,我的500服务器错误已经很高兴停止了感谢DarkBee但我似乎仍然无法正确地将其传递给Twig。

原始问题

我很难在我的Opencart 3.0项目中安装精彩的Mobile_Detect插件(https://github.com/serbanghita/Mobile-Detect)我插入它并成功检查设备是否为桌面设备或移动设备。

我已经学会了将这个php检查传递给twig,我可以将它添加到全局变量中

  $twig = new Twig_Environment($loader);
  $mobTwig->addGlobal('isMobile', true);

或创建一个扩展树枝的公共功能

require_once(DIR_SYSTEM . 'detect/mobiledetectlib/Mobile_Detect.php');
$detect = new Mobile_Detect;
$detectFunction = new function(){
    if($detect->isMobile()){
       return true;
    } else {
       return false;
    }
}

class Project_Twig_Extensions extends Twig_Extension{
    public function getFunctions(){
        return array(new Twig_SimpleFunction('detectDevice', '$detectFunction'));
    }
}

这两个选项都返回500内部服务器错误并立即停止页面加载。在过去的几天里,我无法弄清楚为什么。没有任何谷歌搜索帮助我。有没有人知道为什么会发生这种情况?

php twig opencart
1个回答
0
投票

您发布的代码中存在一些问题:

  1. Anonmynous functions

PHP中你无法实例化new function(),定义一个只使用这种语法的异常函数

$foo = function() {
    return 'foo';
}
echo $foo(); //output: foo
  1. 将变量传递给匿名函数

在你的函数$detectMobile内部,你试图访问变量$detect,但是这个变量没有在anomynous函数的scope中定义。

要在函数中正确使用$detect,您需要使用语言构造use

$detectFunction = new function() use($detect) {
    if ($detect->isMobile()) return true;
    else return false;
}   

您也可以使用global语言结构,但不建议这样做

$detectFunction = new function() {
    global $detect;
    if ($detect->isMobile()) return true;
    else return false;
}   
  1. 传递匿名功能

不要使用quotes来传递实例的变量。

  • 使用new Twig_SimpleFunction('detectDevice', '$detectFunction')将告诉twig运行全局函数$detectFunction
  • 使用new Twig_SimpleFunction('detectDevice', "$detectFunction")将尝试将匿名函数转换为字符串。这是不可能的

传递anonmynous函数的唯一正确方法是传递引用:

new Twig_SimpleFunction('detectDevice', $detectFunction)

现在如果你把所有东西都结合起来就可以成为这样的东

class Project_Twig_Extensions extends Twig_Extension{
    public function getFunctions() {
        $detect = new Mobile_Detect();

        return array(new Twig_SimpleFunction('detectDevice', function() use($detect) {
            if ($detect->isMobile()) return true;
            else return false;
        }));
    }
}

甚至更好

class Project_Twig_Extensions extends Twig_Extension{
    protected $detect = null

    protected function getDetector() {
        if ($this->detect === null) $this->detect = new \Mobile_Detect();
        return $this->detect;
    }

    public function getFunctions() {

        return array(new Twig_SimpleFunction('detectDevice', function() {
            return $this->getDetector()->isMobile();
        }));
    }
}

丢失的纸条请确保在开发过程中使用show all errors ..

© www.soinside.com 2019 - 2024. All rights reserved.