php - 将 php 变量传递到 Twig - 文档抛出错误
问题描述
更新: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);
这也没有传递任何值,感谢 DarkBee,我的 500 服务器错误已经愉快地停止了,但我似乎仍然无法将它正确地传递给 Twig。
原始问题
我在将出色的 Mobile_Detect 插件安装到我的 Opencart 3.0 项目 ( https://github.com/serbanghita/Mobile-Detect ) 时遇到了巨大的麻烦,我已将其插入并成功检查了设备是桌面还是 php 中的移动设备。
我已经学会了为了将这个 php 检查传递给 twig,我可以将它添加到全局变量中
$twig = new Twig_Environment($loader);
$mobTwig->addGlobal('isMobile', true);
或创建一个扩展 twig 的公共函数
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 Internal Server Error 并立即停止页面加载。在过去的几天里,我无法弄清楚为什么。没有多少谷歌搜索对我有帮助。有谁知道为什么会发生这种情况?
解决方案
您发布的代码中有很多问题:
在PHP
您无法实例化 anew function()
时,要定义异常函数,您只需使用此语法
$foo = function() {
return 'foo';
}
echo $foo(); //output: foo
- 将变量传递给异常函数
在您$detectMobile
尝试访问变量的函数内部$detect
,该变量未在异常函数的范围内定义。
要在函数内部正确使用$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;
}
- 传递异常函数
不要使用引号来传递实例变量。
使用
new Twig_SimpleFunction('detectDevice', '$detectFunction')
将告诉twig
运行全局函数$detectFunction
使用
new Twig_SimpleFunction('detectDevice', "$detectFunction")
将尝试将异常函数转换为字符串。这是不可能的
传递匿名函数的唯一正确方法是传递引用:
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();
}));
}
}
在丢失的笔记中,请确保在开发时显示所有错误。
推荐阅读
- racket - 语法中的歧义
- node.js - ReactNative MQTT 监听器
- c# - 无法使用 MVC (.Net core MVC 3.1) 运行 IdentityServer 4 (.Net core 3.1)
- excel - 我正在编写 VBA 并在这行代码中不断遇到语法错误
- visual-studio - 如何从 powershell 打开解决方案文件?
- html - 将 html 元素动态添加到 Razor 页面,Asp.Net Core
- amazon-web-services - 我可以在没有浏览器的情况下在命令行使用 sso 获取 aws 帐户的凭据吗
- python - 来自字节或字符串的 Numpy 重新数组
- python - 用于 formData 类型的 Python REST 客户端
- twilio - 将扩展从 Twilio 传递到 Asterisk