I'm developing a site on a platform that's using the Twig (Symfony) language for its templates, and in one place, I need to change the layout (disable a default block and load a different block) based on whether the user is on a mobile device or a desktop.
我正在一个使用Twig(Symfony)语言作为模板的平台上开发一个网站,在一个地方,我需要根据用户是在移动设备上还是在桌面上更改布局(禁用默认块并加载不同的块)。
I know how to do it in PHP (using the "check_user_agent('mobile')" variable), but that doesn't work in Twig... and I've come across a reference to the Twig "Mobile Detect Bundle", but I have no idea how to install it (shared hosting with cPanel).
我知道如何在PHP中做到这一点(使用“check_user_agent('mobile')”变量),但这在Twig中不起作用。。。我遇到了一个关于Twig“Mobile Detect Bundle”的参考,但我不知道如何安装它(与cPanel共享托管)。
Soo... is there a way to detect mobile user-agent in Twig, without having to install anything?
Soo。。。有没有一种方法可以检测Twig中的移动用户代理,而不必安装任何东西?
更多回答
优秀答案推荐
General Solution for PHP
https://github.com/serbanghita/Mobile-Detect/ is a great and maintained php class to detect the user agent and is not limited to Symfony.
https://github.com/serbanghita/Mobile-Detect/是一个很好的维护php类,用于检测用户代理,并且不限于Symfony。
For Symfony
To use the above class with Symfony, you could either write a twig extension yourself or use this Mobile Detect Twig Extension that does the job.
要将上面的类与Symfony一起使用,您可以自己编写一个twig扩展,也可以使用这个Mobile Detect twig扩展来完成这项工作。
During each request, Symfony will set a global template variable app in both Twig and PHP template engines by default.
The Request object that represents the current request: app.request
在每次请求过程中,Symfony将默认在Twig和PHP模板引擎中设置一个全局模板变量应用程序。表示当前请求的Request对象:app.Request
So if you want to know the user-agent you can use app.request.headers
in the template.
e.g :
因此,如果您想了解用户代理,可以在模板中使用app.request.headers。例如:
{{ app.request.headers.get('User-Agent')}}
If you already have PHP code to return true/false based on the user agent, it is quite simple to Write a custom Twig Extension to run that code, but from Twig.
如果您已经有了基于用户代理返回true/false的PHP代码,那么编写一个自定义的Twig扩展来运行该代码非常简单,但要从Twig运行。
Alternatively, you can run the check in the controller and pass in the result, or in a 'kernel.controller Event' to even run the check before a controller action is called (probably putting it into a Request 'attributes', where it can also be checked in the template).
或者,您可以在控制器中运行检查并传递结果,或者在“kernel.controller Event”中运行检查,甚至在调用控制器操作之前运行检查(可能将其放入请求“attributes”中,也可以在模板中进行检查)。
If you dont want to use the Mobile Detect Bundle and you want to detect mobile in Twig here is a solution:
如果你不想使用移动检测捆绑包,并且你想在Twig中检测移动设备,这里有一个解决方案:
{% if 'iPhone' in app.request.headers.get('User-Agent')
or 'iPod' in app.request.headers.get('User-Agent')
or 'iPad' in app.request.headers.get('User-Agent')
or 'Android' in app.request.headers.get('User-Agent')
or 'BlackBerry' in app.request.headers.get('User-Agent') %}
<h1>Hi, I see this using Mobile Device</h1>
{% else %}
<h1>Hi, I see this because im not using Mobile Device</h1>
{% endif %}
更多回答
I think @snela, @alisterbulman is right... You can resolve your problem with Twig Extension
.
我认为@snella,alisterbulman是对的。。。您可以使用Twig Extension解决问题。
我是一名优秀的程序员,十分优秀!