Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bes89/mobiledetect-twig-extension
MobileDetect-integration for Twig
https://github.com/bes89/mobiledetect-twig-extension
Last synced: 21 days ago
JSON representation
MobileDetect-integration for Twig
- Host: GitHub
- URL: https://github.com/bes89/mobiledetect-twig-extension
- Owner: bes89
- License: mit
- Created: 2013-10-03T16:24:04.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-07-28T13:11:26.000Z (almost 2 years ago)
- Last Synced: 2024-05-14T08:02:50.925Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 7.81 KB
- Stars: 19
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- awesome-twig - MobileDetect - MobileDetect-integration for Twig (Twig / Extensions)
- awesome-twig - MobileDetect - MobileDetect-integration for Twig (Twig / Extensions)
README
mobiledetect-twig-extension
==========================="Mobile Detect" integration for Twig
Installation
------------```bash
composer require "bes/mobiledetect-twig-extension:1.*"
```And register the extension:
**Twig standalone**
```php
$twig->addExtension(new Bes\Twig\Extension\MobileDetectExtension());
```**Silex**
Yay, you don't need a ServiceProvider for it!
Add the following code after registering TwigServiceProvider:
```php
$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//Bundle/Resources/config/services.yml` or
globally in `app/config/config.yml`:```yaml
services:
twig.mobile_detect_extension:
class: Bes\Twig\Extension\MobileDetectExtension
tags:
- { name: twig.extension }
```... and you are done!
Examples
--------Render different layouts:
```jinja
{% extends is_mobile() ? "layout_mobile.html.twig" : "layout.html.twig" %}
```Check device type:
```jinja
{% if is_mobile() %} ... {% endif %}
{% if is_tablet() %} ... {% endif %}
```Or:
```jinja
{% if is_mobile() and is_samsung() %} ... {% endif %}
```You can get a list of all the known devices with:
```jinja
{{ get_available_devices()|join("
")|raw }}
```