PHPackages                             reun/twig-utilities - 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. reun/twig-utilities

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

reun/twig-utilities
===================

Various Twig utilities and extensions

0.14.1(4mo ago)045MITPHPPHP ^8.2CI passing

Since Apr 24Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/ReunMedia/twig-utilities)[ Packagist](https://packagist.org/packages/reun/twig-utilities)[ RSS](/packages/reun-twig-utilities/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (14)Versions (25)Used By (0)

Twig utilities
==============

[](#twig-utilities)

[![Supported PHP Version](https://camo.githubusercontent.com/6d9206a80f9bc6a9abf3f819d46f89b27a296762fed756cd3b63091d672190d5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7265756e2f747769672d7574696c69746965732f5048503f6c6f676f3d504850266c6f676f436f6c6f723d37373742423326636f6c6f723d373737424233)](https://www.php.net/supported-versions.php)[![Packagist Version](https://camo.githubusercontent.com/27a956bc2e9d20f35b6594d5b1637ca47476964ea26ad2869713786f51098796/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7265756e2f747769672d7574696c6974696573)](https://packagist.org/packages/reun/twig-utilities)

Various utility filters, functions and Slim integration for Twig.

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

[](#installation)

Install package using Composer

```
composer require reun/twig-utilities
```

### Included container definitions

[](#included-container-definitions)

This package includes container definitions for [Reun Media PHP app template](https://github.com/ReunMedia/php-app-template).

```
$c += TwigUtilitiesDefinitions::getDefinitions();
```

Usage - Filters and functions
-----------------------------

[](#usage---filters-and-functions)

Functions and filters are defined in separate classes and provide either `getFilter()` or `getFunction()` static method that can be used to add them to Twig. The class name beginning with a lowercase character becomes the name of the filter/function in Twig. E.g. `CopyrightYear` becomes `copyrightYear()`.

The recommended way is to create a new Twig extension for your project and add filters and functions in `getFilters()` and `getFunctions()`.

```
use Reun\TwigUtilities\Filters\Htmlpurify;
use Reun\TwigUtilities\Functions\CopyrightYear;
use Twig\Extension\AbstractExtension;

class MyExtension extends AbstractExtension
{
  public function getFilters(): array
  {
    return [
      Htmlpurify::getFilter(),
    ];
  }

  public function getFunctions(): array
  {
    return [
      CopyrightYear::getFunction(),
    ]
  }
}

// Add extension to Twig.
$twig->addExtension(new MyExtension());
```

Some filters and functions have external dependencies. To use them you must add a [runtime loader](https://twig.symfony.com/doc/3.x/advanced.html#definition-vs-runtime) to Twig environment.

```
use DI\Container;
use Twig\RuntimeLoader\ContainerRuntimeLoader;

$twig->addRuntimeLoader(new ContainerRuntimeLoader(new Container()));
```

### Creating new filters and functions

[](#creating-new-filters-and-functions)

Just extend either `Reun\TwigUtilities\Filters\AbstractFilter` or `Reun\TwigUtilities\Functions\AbstractFunction` and implement `__invoke()`. Options can be specified by overriding `getOptions()` method.

Available features
------------------

[](#available-features)

### Filters

[](#filters)

htmlpurifySanitizes a string with [HTML Purifier](http://htmlpurifier.org/)### Functions

[](#functions)

copyrightYearDynamic year rangeformatDateRangeFormat date and time according to Finnish date conventions[viteAsset](docs/ViteAsset.md)Add CSS and JS assets built by Vite to Twig templates### Slim utilities

[](#slim-utilities)

[DynamicTwigPage](docs/DynamicTwigPage.md)Dynamic Slim routing to Twig templates with optional dataStaticTwigPageMap route to a specific Twig template with optional data[TwigErrorRenderer](docs/TwigErrorRenderer.md)Render errors as Twig templatesFAQ and notes
-------------

[](#faq-and-notes)

### Where is the Markdown filter?

[](#where-is-the-markdown-filter)

Use Twig's own [`markdown_to_html`](https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html)filter.

### Handling dates and timezones

[](#handling-dates-and-timezones)

It is recommended to handle all dates and times as `UTC` and use that as the PHP timezone setting. Twig's builtin [`date`](https://twig.symfony.com/doc/3.x/filters/date.html) filter should be used to output dates in a different timezone and can be combined with `formatDateRange()` etc. See [Twig's documentation on how to set the timezone](https://twig.symfony.com/doc/3.x/filters/date.html#timezone).

Example of formatting event times in different timezone:

```

  {{ formatDateRange(event.startDate|date, event.endDate|date) }}

```

Development
-----------

[](#development)

Run `composer dev` to start a test server. The code is located in `tests/functional/TwigUtilities`.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance74

Regular maintenance activity

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~121 days

Recently: every ~98 days

Total

24

Last Release

142d ago

PHP version history (4 changes)v0.1.0PHP ^7.0

v0.2.0PHP ^7.1

v0.9.0PHP ^7.4

0.10.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/bb53e463857ab3f210876fa35ffca0c019850ad77851c8963ccacb0264ef1d5a?d=identicon)[Cryszon](/maintainers/Cryszon)

---

Top Contributors

[![Cryszon](https://avatars.githubusercontent.com/u/5220970?v=4)](https://github.com/Cryszon "Cryszon (108 commits)")

---

Tags

library

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/reun-twig-utilities/health.svg)

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M314](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M220](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M132](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M53](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M161](/packages/symfony-ux-twig-component)

PHPackages © 2026

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