Stars: 103
Forks: 18
Pull Requests: 10
Issues: 10
Watchers: 8
Last Updated: 2021-06-16 14:47:32
A number of useful filters for Twig
License: MIT License
Languages: PHP
A number of useful filters for Twig.
Jasny's Twig Extensions can be easily installed using composer
composer require jasny/twig-extensions
$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Jasny\Twig\DateExtension());
$twig->addExtension(new Jasny\Twig\PcreExtension());
$twig->addExtension(new Jasny\Twig\TextExtension());
$twig->addExtension(new Jasny\Twig\ArrayExtension());
To use in a symfony project register the extensions as a service.
services:
twig.extension.date:
class: Jasny\Twig\DateExtension
tags:
- { name: twig.extension }
twig.extension.pcre:
class: Jasny\Twig\PcreExtension
tags:
- { name: twig.extension }
twig.extension.text:
class: Jasny\Twig\TextExtension
tags:
- { name: twig.extension }
twig.extension.array:
class: Jasny\Twig\ArrayExtension
tags:
- { name: twig.extension }
Format a date base on the current locale. Requires the intl extension.
Locale::setDefault(LC_ALL, "en_US"); // vs "nl_NL"
{{"now"|localdate('long')}} <!-- July 12, 2013 --> <!-- 12 juli 2013 -->
{{"now"|localtime('short')}} <!-- 5:53 PM --> <!-- 17:53 -->
{{"2013-10-01 23:15:00"|localdatetime}} <!-- 10/01/2013 11:15 PM --> <!-- 01-10-2013 23:15 -->
{{"22-08-1981"|age}} <!-- 35 -->
{{ 3600|duration }} <!-- 1h -->
Exposes PCRE to Twig.
{% if client.email|preg_match('/^.+@.+\.\w+$/') %}Email: {{ client.email }}{% endif %}
Website: {{ client.website|preg_replace('~^https?://~')
First name: {{ client.fullname|preg_get('/^\S+/') }}
<ul>
{% for item in items|preg_split('/\s+/')|preg_filter('/-test$/', 'invert') %}
<li>{{ item }}</li>
{% endfor %}
</ul>
Convert text to HTML + string functions
Brings PHP's array functions to Twig