PHPackages                             jasny/twig-extensions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. jasny/twig-extensions

ActiveLibrary[Templating &amp; Views](/categories/templating)

jasny/twig-extensions
=====================

A set of useful Twig filters

v1.3.1(1y ago)10710.2M—5.2%19[1 issues](https://github.com/jasny/twig-extensions/issues)8MITPHPPHP &gt;=7.4.0CI failing

Since Feb 7Pushed 2mo ago6 watchersCompare

[ Source](https://github.com/jasny/twig-extensions)[ Packagist](https://packagist.org/packages/jasny/twig-extensions)[ Docs](http://github.com/jasny/twig-extensions#README)[ RSS](/packages/jasny-twig-extensions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (8)Used By (8)

Jasny Twig Extensions
=====================

[](#jasny-twig-extensions)

[![PHP](https://github.com/jasny/twig-extensions/actions/workflows/php.yml/badge.svg)](https://github.com/jasny/twig-extensions/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/bc749df5af5f4f2e09e4158466fd8db91c585d1180770d37ab26d2c1c58e655c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f747769672d657874656e73696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/twig-extensions/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/1aec11f41352219f9b6f42902c16bae60d6b3549dd14adb3ba2acbb2b91c2ecb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f747769672d657874656e73696f6e732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/twig-extensions/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/c8fbcdce80080fa9b5f7ef2ce6323ea4c0953be5491b283ab47c34463ecb6d48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f747769672d657874656e73696f6e732e737667)](https://packagist.org/packages/jasny/twig-extensions)[![Packagist License](https://camo.githubusercontent.com/99fa72b32ff2dcac5276bb9e068852d71d1ab1da7fb987ee13bdffd135d75f8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f747769672d657874656e73696f6e732e737667)](https://packagist.org/packages/jasny/twig-extensions)

A number of useful filters for Twig.

Installation
------------

[](#installation)

Jasny's Twig Extensions can be easily installed using [composer](http://getcomposer.org/)

```
composer require jasny/twig-extensions

```

Usage
-----

[](#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](http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-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 }
```

Date extension
--------------

[](#date-extension)

Format a date based on the current locale. Requires the [intl extension](http://www.php.net/intl).

- 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')}}
{{"now"|localtime('short')}}
{{"2013-10-01 23:15:00"|localdatetime}}
{{"22-08-1981"|age}}
{{ 3600|duration }}

```

PCRE
----

[](#pcre)

Exposes [PCRE](http://www.php.net/pcre) to Twig.

- 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+/') }}

  {% for item in items|preg_split('/\s+/')|preg_filter('/-test$/', 'invert') %}
    {{ item }}
  {% endfor %}

```

Text
----

[](#text)

Convert text to HTML + string functions

- 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
-----

[](#array)

Brings PHP's array functions to Twig

- 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

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance62

Regular maintenance activity

Popularity61

Solid adoption and visibility

Community29

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 77.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~643 days

Recently: every ~697 days

Total

7

Last Release

623d ago

PHP version history (5 changes)1.0.0PHP &gt;=5.3.0

v1.1.0PHP &gt;=5.6.0

v1.2.0PHP &gt;=7.0.0 | &gt;=5.6.0

v1.3.0PHP &gt;=7.0.0

v1.3.1PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (56 commits)")[![inoas](https://avatars.githubusercontent.com/u/20972207?v=4)](https://github.com/inoas "inoas (9 commits)")[![mariuszkaczanowski](https://avatars.githubusercontent.com/u/443852?v=4)](https://github.com/mariuszkaczanowski "mariuszkaczanowski (2 commits)")[![othercorey](https://avatars.githubusercontent.com/u/24221186?v=4)](https://github.com/othercorey "othercorey (1 commits)")[![sroze](https://avatars.githubusercontent.com/u/804625?v=4)](https://github.com/sroze "sroze (1 commits)")[![ubermichael](https://avatars.githubusercontent.com/u/1328893?v=4)](https://github.com/ubermichael "ubermichael (1 commits)")[![mickadoo](https://avatars.githubusercontent.com/u/6374064?v=4)](https://github.com/mickadoo "mickadoo (1 commits)")[![OndraM](https://avatars.githubusercontent.com/u/793041?v=4)](https://github.com/OndraM "OndraM (1 commits)")

---

Tags

datetimepcrephptwigarraydatetimetimedatetemplatingregextextPCREpreg

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jasny-twig-extensions/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-twig-extensions/health.svg)](https://phpackages.com/packages/jasny-twig-extensions)
```

###  Alternatives

[composer/pcre

PCRE wrapping library that offers type-safe preg\_\* replacements.

699313.8M34](/packages/composer-pcre)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[nesbot/carbon

An API extension for DateTime that supports 281 different languages.

177661.4M4.8k](/packages/nesbot-carbon)[carbonphp/carbon-doctrine-types

Types to use Carbon in Doctrine

218220.4M8](/packages/carbonphp-carbon-doctrine-types)[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
