bes/mobiledetect-twig-extension

MobileDetect-integration for Twig

Installs: 196 786

Dependents: 0

Suggesters: 0

Security: 0

Stars: 19

Watchers: 3

Forks: 8

Open Issues: 2

Type:twig-extension

v1.0.3 2017-11-05 21:51 UTC

This package is not auto-updated.

Last update: 2024-03-25 13:10:03 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 }}