PHPackages                             craue/twigextensions-bundle - 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. craue/twigextensions-bundle

ActiveSymfony-bundle[Templating &amp; Views](/categories/templating)

craue/twigextensions-bundle
===========================

Useful Twig extensions for your Symfony project.

2.9.0(4y ago)81217.1k↑120.1%12[1 issues](https://github.com/craue/TwigExtensionsBundle/issues)2MITPHPPHP ^7.3|^8CI failing

Since Feb 28Pushed 4y ago4 watchersCompare

[ Source](https://github.com/craue/TwigExtensionsBundle)[ Packagist](https://packagist.org/packages/craue/twigextensions-bundle)[ Docs](https://github.com/craue/TwigExtensionsBundle)[ RSS](/packages/craue-twigextensions-bundle/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (10)Versions (21)Used By (2)

Information
===========

[](#information)

[![Build Status](https://camo.githubusercontent.com/762ee463c3cc6444600439730fecc5c791293cf700c575ea624a827a17412c0c/68747470733a2f2f7472617669732d63692e6f72672f63726175652f54776967457874656e73696f6e7342756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/craue/TwigExtensionsBundle)

TwigExtensionsBundle is a collection of useful Twig extensions for your Symfony project.

A live demo with code examples can is available at .

DecorateEmptyValueExtension
---------------------------

[](#decorateemptyvalueextension)

Provides an enhanced `default` filter, `craue_default`, to decorate empty values with a placeholder which can even be HTML.

Usually, if you want to use HTML, e.g. the HTML entity `&mdash;`, as value for the default filter in an HTML Twig template you have to do cumbersome

```
{{ somevalue | e | default('&mdash;') | raw }}
```

to make it render properly. With this extension you can write

```
{{ somevalue | craue_default }}
```

instead.

ArrayHelperExtension
--------------------

[](#arrayhelperextension)

Provides the filters

- `craue_without` wrapping PHP's `array_diff` function,
- `craue_replaceKey` which adds/replaces an array entry (whereupon the key can be a variable),
- `craue_removeKey` which removes an array entry by key (whereupon the key can be a variable), and
- `craue_translateArray` which translates all entries in an array.

FormExtension
-------------

[](#formextension)

Provides a mechanism to render a form several times on one page. This is done by cloning the form prior to rendering using the `craue_cloneForm` function.

StringHelperExtension
---------------------

[](#stringhelperextension)

Provides the `craue_trailingDot` filter for ensuring that a text ends with a dot. This comes in handy when using error messages (e.g. for validation) of vendor bundles (which are written like sentences but are missing the trailing dots) together with your own ones (which should include the trailing dot).

FormatDateTimeExtension
-----------------------

[](#formatdatetimeextension)

Provides the filters `craue_date`, `craue_time`, and `craue_datetime` for locale-aware formatting of date, time, and date/time values.

FormatNumberExtension
---------------------

[](#formatnumberextension)

Provides the filters `craue_number`, `craue_currency`, and `craue_spellout` for locale-aware formatting of numbers and currencies.

ChangeLanguageExtension
-----------------------

[](#changelanguageextension)

Provides the functions `craue_languageName` and `craue_availableLocales` as well as a template for implementing a language change mechanism.

Installation
============

[](#installation)

Get the bundle
--------------

[](#get-the-bundle)

Let Composer download and install the bundle by running

```
composer require craue/twigextensions-bundle
```

in a shell.

Enable the bundle
-----------------

[](#enable-the-bundle)

If you don't use Symfony Flex, register the bundle manually:

```
// in config/bundles.php
return [
	// ...
	Craue\TwigExtensionsBundle\CraueTwigExtensionsBundle::class => ['all' => true],
];
```

Or, for Symfony 3.4:

```
// in app/AppKernel.php
public function registerBundles() {
	$bundles = [
		// ...
		new Craue\TwigExtensionsBundle\CraueTwigExtensionsBundle(),
	];
	// ...
}
```

Examples to use the extensions in your Twig template
====================================================

[](#examples-to-use-the-extensions-in-your-twig-template)

DecorateEmptyValueExtension
---------------------------

[](#decorateemptyvalueextension-1)

```
{{ someValueWhichMayBeEmpty | craue_default }}
{{ someValueWhichMayBeEmpty | craue_default('no value') }}
{{ someValueWhichMayBeEmpty | craue_default('&ndash;') }}
{{ someValueWhichMayBeEmpty | craue_default(0) }}
```

ArrayHelperExtension
--------------------

[](#arrayhelperextension-1)

```
{{ anArray | craue_without(aValueOrAnArray) | join(', ') }}
{{ ['red', 'green', 'yellow', 'blue'] | craue_without('yellow') | join(', ') }} will print "red, green, blue"
{{ ['red', 'green', 'yellow', 'blue'] | craue_without(['yellow', 'black', 'red']) | join(', ') }} will print "green, blue"

{{ anArray | craue_replaceKey(key, value) | join(', ') }}
{% set newKey = 'key3' %}
{{ {'key1': 'value1', 'key2': 'value2'} | craue_replaceKey(newKey, 'value3') | join(', ') }} will print "value1, value2, value3"

{{ anArray | craue_removeKey(key) | join(', ') }}
{{ {'key1': 'value1', 'key2': 'value2'} | craue_removeKey('key1') | join(', ') }} will print "value2"

{{ anArray | craue_translateArray() | join(', ') }}
```

FormExtension
-------------

[](#formextension-1)

```
{% for myEntity in myEntities %}
	{% set myFormInstance = craue_cloneForm(myForm) %}

		{{ form_widget(myFormInstance) }}

{% endfor %}
```

StringHelperExtension
---------------------

[](#stringhelperextension-1)

```
{{ aString | craue_trailingDot }}
{{ 'This text should end with a dot' | craue_trailingDot }}
{{ 'This text should end with exactly one dot.' | craue_trailingDot }}
```

FormatDateTimeExtension
-----------------------

[](#formatdatetimeextension-1)

```
with the current locale
date: {{ someDateTimeValue | craue_date }}
time: {{ someDateTimeValue | craue_time }}
both: {{ someDateTimeValue | craue_datetime }}

with a specific locale
date: {{ someDateTimeValue | craue_date('de-DE') }}
time: {{ someDateTimeValue | craue_time('de') }}
both: {{ someDateTimeValue | craue_datetime('en-GB') }}
```

FormatNumberExtension
---------------------

[](#formatnumberextension-1)

```
with the current locale
thousands separator: {{ someNumber | craue_number }}
default currency: {{ someNumber | craue_currency }}
specific currency: {{ someNumber | craue_currency('EUR') }}
spelled out number: {{ someNumber | craue_spellout }}

with a specific locale
thousands separator: {{ someNumber | craue_number('de-DE') }}
default currency: {{ someNumber | craue_currency(null, 'de-DE') }}
specific currency: {{ someNumber | craue_currency('EUR', 'de-DE') }}
spelled out number: {{ someNumber | craue_spellout('de-DE') }}
```

ChangeLanguageExtension
-----------------------

[](#changelanguageextension-1)

There's a Twig template provided which you can use to render a "change language" menu like this:

```
{% include '@CraueTwigExtensions/ChangeLanguage/changeLanguage.html.twig' %}
```

This will render a list of links to the current route in all defined languages. Wrap it in a div to style it via CSS. Take a look at the template if you want to customize it.

Set/override default values
===========================

[](#setoverride-default-values)

DecorateEmptyValueExtension
---------------------------

[](#decorateemptyvalueextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.decorateEmptyValue.placeholder: &ndash;
```

FormatDateTimeExtension
-----------------------

[](#formatdatetimeextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.formatDateTime.datetype: full
  craue_twig_extensions.formatDateTime.timetype: short
  craue_twig_extensions.formatDateTime.timeZone: Europe/Berlin
```

FormatNumberExtension
---------------------

[](#formatnumberextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.formatNumber.currency: EUR
```

ChangeLanguageExtension
-----------------------

[](#changelanguageextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.availableLocales: [de, en, ru]
  craue_twig_extensions.changeLanguage.showForeignLanguageNames: true
  craue_twig_extensions.changeLanguage.showFirstUppercase: false
```

You can also set the keys to be more specific about the locales:

```
# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.availableLocales:
    de_DE: de
    en: en
    ru: ru
```

```

	de
	en
	ru

```

Advanced stuff
==============

[](#advanced-stuff)

Aliases
-------

[](#aliases)

Optionally, you can define aliases for all provided filters/functions to be used within your project. This allows you to use names you prefer instead of the pre-defined ones. E.g., if you don't like to write

```
{{ somevalue | craue_default }}
```

all the time, you may define an alias like `d` for the `craue_default` filter which allows you to write

```
{{ somevalue | d }}
```

in your Twig templates. But pay attention to not accidentally override built-in filters/functions, although you can do it intentionally.

### DecorateEmptyValueExtension

[](#decorateemptyvalueextension-3)

```
# in app/config/parameters.yml
  craue_twig_extensions.decorateEmptyValue.filterAlias: d
```

### ArrayHelperExtension

[](#arrayhelperextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.arrayHelper.withoutAlias: without
  craue_twig_extensions.arrayHelper.replaceKeyAlias: replaceKey
  craue_twig_extensions.arrayHelper.removeKeyAlias: removeKey
  craue_twig_extensions.arrayHelper.translateArrayAlias: translateArray
```

### FormExtension

[](#formextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.form.cloneFormAlias: cloneForm
```

### StringHelperExtension

[](#stringhelperextension-2)

```
# in app/config/parameters.yml
  craue_twig_extensions.stringHelper.trailingDotAlias: trailingDot
```

### FormatDateTimeExtension

[](#formatdatetimeextension-3)

```
# in app/config/parameters.yml
  craue_twig_extensions.formatDateTime.dateFilterAlias: date
  craue_twig_extensions.formatDateTime.timeFilterAlias: time
  craue_twig_extensions.formatDateTime.dateTimeFilterAlias: datetime
```

### FormatNumberExtension

[](#formatnumberextension-3)

```
# in app/config/parameters.yml
  craue_twig_extensions.formatNumber.numberFilterAlias: number
  craue_twig_extensions.formatNumber.currencyFilterAlias: currency
  craue_twig_extensions.formatNumber.spelloutFilterAlias: spellout
```

### ChangeLanguageExtension

[](#changelanguageextension-3)

```
# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.languageNameAlias: languageName
  craue_twig_extensions.changeLanguage.availableLocalesAlias: availableLocales
```

Enabling only specific extensions
---------------------------------

[](#enabling-only-specific-extensions)

By default, all provided extensions are enabled. If you're using only one or some of them, you may want to disable the others. The following enables them all, so remove the ones you don't need:

```
# in app/config/config.yml
craue_twig_extensions:
  enable_only:
    - ArrayHelperExtension
    - ChangeLanguageExtension
    - DecorateEmptyValueExtension
    - FormatDateTimeExtension
    - FormatNumberExtension
    - FormExtension
    - StringHelperExtension
```

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 98.1% 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 ~174 days

Recently: every ~188 days

Total

20

Last Release

1594d ago

Major Versions

1.0.2 → 2.0.02014-06-01

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

2.1.1PHP ^5.3.2|~7.0

2.3.0PHP ^5.3.9|~7.0

2.4.0PHP ~7.0

2.8.0PHP ^7.3|^8

### Community

Maintainers

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

---

Top Contributors

[![craue](https://avatars.githubusercontent.com/u/800119?v=4)](https://github.com/craue "craue (367 commits)")[![yberkholz](https://avatars.githubusercontent.com/u/1708117?v=4)](https://github.com/yberkholz "yberkholz (5 commits)")[![rrehbein](https://avatars.githubusercontent.com/u/140358?v=4)](https://github.com/rrehbein "rrehbein (2 commits)")

---

Tags

bundlephpsymfonysymfony-bundletwig-extensionsymfonytwigextensions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/craue-twigextensions-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/craue-twigextensions-bundle/health.svg)](https://phpackages.com/packages/craue-twigextensions-bundle)
```

###  Alternatives

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k18.3M417](/packages/easycorp-easyadmin-bundle)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1617.3k16](/packages/2lenet-crudit-bundle)

PHPackages © 2026

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