bes / mobiledetect-twig-extension
MobileDetect-integration for Twig
Installs: 223 021
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 2
Forks: 8
Open Issues: 2
Type:twig-extension
Requires
- php: >=5.3.0
- mobiledetect/mobiledetectlib: ~2
- twig/twig: ~2
This package is not auto-updated.
Last update: 2025-04-07 18:05:25 UTC
README
"Mobile Detect" integration for Twig
Installation
composer require "bes/mobiledetect-twig-extension:1.*"
And register the extension:
Twig standalone
$twig->addExtension(new Bes\Twig\Extension\MobileDetectExtension());
Silex
Yay, you don't need a ServiceProvider for it!
Add the following code after registering TwigServiceProvider:
$app['twig'] = $app->share($app->extend('twig', function($twig) { /* @var $twig \Twig_Environment */ $twig->addExtension(new Bes\Twig\Extension\MobileDetectExtension); return $twig; }));
... and you are done!
Symfony3
Yay, you don't need a Bundle for it!
Add the following code to one of your services.yml, e.g.
src/<vendor>/<your>Bundle/Resources/config/services.yml
or
globally in app/config/config.yml
:
services: twig.mobile_detect_extension: class: Bes\Twig\Extension\MobileDetectExtension tags: - { name: twig.extension }
... and you are done!
Examples
Render different layouts:
{% extends is_mobile() ? "layout_mobile.html.twig" : "layout.html.twig" %}
Check device type:
{% if is_mobile() %} ... {% endif %} {% if is_tablet() %} ... {% endif %}
Or:
{% if is_mobile() and is_samsung() %} ... {% endif %}
You can get a list of all the known devices with:
{{ get_available_devices()|join("<br />")|raw }}