[英]how to detect screen size or mobile/desktop using twig
我目前正在使用twig作为我的模板引擎,我想在使用移动和桌面加载网站时加载不同的图像网址。 是否有捷径可寻?
所以我想做这样的事情:
{% if (mobile) %}
<img src="{{ picture.getLowresimageurl() }}"/>
{% else %}
<img src="{{ picture.getMedresimageurl() }}"/>
{% endif %}
有没有办法做到这一点?
您可以使用MobileDetectBundle检测移动设备,管理移动视图并重定向到移动设备和平板电脑版本
Twig Helper
{% if is_mobile() %}
{% if is_tablet() %}
{% if is_device('iphone') %} # magic methods is[...]
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 %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.