2017 © Pedro Peláez
 

library twig-extensions

A set of useful Twig filters

image

jasny/twig-extensions

A set of useful Twig filters

  • Wednesday, September 13, 2017
  • by jasny
  • Repository
  • 6 Watchers
  • 76 Stars
  • 491,697 Installations
  • PHP
  • 8 Dependents
  • 2 Suggesters
  • 21 Forks
  • 3 Open issues
  • 6 Versions
  • 39 % Grown

The README.md

Jasny Twig Extensions

PHP Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License, (*1)

A number of useful filters for Twig., (*2)

Installation

Jasny's Twig Extensions can be easily installed using composer, (*3)

composer require jasny/twig-extensions

Usage

$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., (*4)

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 }

Date extension

Format a date based on the current locale. Requires the intl extension., (*5)

  • localdate - Format the date value as a string based on the current locale
  • localtime - Format the time value as a string based on the current locale
  • localdatetime - Format the date/time value as a string based on the current locale
  • age - Get the age (in years) based on a date
  • duration - Get the duration string from seconds
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 -->

PCRE

Exposes PCRE to Twig., (*6)

  • preg_quote - Quote regular expression characters
  • preg_match - Perform a regular expression match
  • preg_get - Perform a regular expression match and return the matched group
  • preg_get_all - Perform a regular expression match and return the group for all matches
  • preg_grep - Perform a regular expression match and return an array of entries that match the pattern
  • preg_replace - Perform a regular expression search and replace
  • preg_filter - Perform a regular expression search and replace, returning only matched subjects.
  • preg_split - Split text into an array using a regular expression
{% 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>

Text

Convert text to HTML + string functions, (*7)

  • paragraph - Add HTML paragraph and line breaks to text
  • line - Get a single line of text
  • less - Cut of text on a page break
  • truncate - Cut off text if it's too long
  • linkify - Turn all URLs into clickable links (also supports Twitter @user and #subject)

Array

Brings PHP's array functions to Twig, (*8)

  • sum - Calculate the sum of values in an array
  • product - Calculate the product of values in an array
  • values - Return all the values of an array
  • as_array - Cast an object to an associated array
  • html_attr - Turn an array into an HTML attribute string

The Versions

13/09 2017

dev-master

9999999-dev http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

The Development Requires

templating array date text time datetime regex pcre preg

13/09 2017

v1.2.0

1.2.0.0 http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

The Development Requires

templating array date text time datetime regex pcre preg

08/02 2017

v1.1.0

1.1.0.0 http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

The Development Requires

templating array date text time datetime regex pcre preg

12/01 2017

v1.0.2

1.0.2.0 http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

The Development Requires

templating array date text time datetime regex pcre preg

09/12 2015

v1.0.1

1.0.1.0 http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

templating array date text time datetime regex pcre preg

07/02 2014

1.0.0

1.0.0.0 http://github.com/jasny/twig-extensions#README

A set of useful Twig filters

  Sources   Download

MIT

The Requires

 

templating array date text time datetime regex pcre preg