如何使用树枝检测屏幕大小或移动/桌面

adi*_*dit 7 html javascript css symfony twig

我目前正在使用twig作为我的模板引擎,我想在使用移动和桌面加载网站时加载不同的图像网址.是否有捷径可寻?

所以我想做这样的事情:

{% if (mobile) %}
   <img src="{{ picture.getLowresimageurl() }}"/>
{% else %}
   <img src="{{ picture.getMedresimageurl() }}"/>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

Pic*_*oss 6

您可以使用MobileDetectBundle检测移动设备,管理移动视图并重定向到移动设备和平板电脑版本

Twig Helper

{% if is_mobile() %}
{% if is_tablet() %}
{% if is_device('iphone') %} # magic methods is[...]
Run Code Online (Sandbox Code Playgroud)

Twig的例子

{% if is_mobile_view() %}
    {% extends "MyBundle:Layout:mobile.html.twig" %}
{% else if is_tablet_view() %}
    {% extends "MyBundle:Layout:tablet.html.twig" %}
{% else if is_full_view() or is_not_mobile_view() %}
    {% extends "MyBundle:Layout:full.html.twig" %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)