ARCJSCHMIDT.de
Thoughts from a developer//entrepreneur.
Founder of AETROS.com

open-source

php|Propel1 php|Propel2 php|CDS php|PHP-PM php|php-pm-httpkernel web|Jarves.io web|css-element-queries js|jQuery-selectBox php|PropelBundle php|Propel Sandbox js|angular2-localStorage js|angular-es6-annotations php|php-rest-service php|topsort.php xxx|BetterQuitJobBundle js|angular-typescript-decorators c++|node-core-audio php|optimistic-locking-behavior php|change-logger-behavior

github.com/marcj twitter.com/MarcJSchmidt plus.google.com/+MarcJSchmidt RSS xing.com/profile/MarcJ_Schmidt

Twig dynamic filters / from variable.

12 December 2013, by Marc

In Twig it’s not possible to call filters based on a variable per default. I’ve created therefore a small Symfony bundle that provides a new Twig filter apply_filter which does exactly that: Calls filter based on a variable. Useful when you want to give your users a way to format something and therefore the filters comes from the database.

Download

You can download and install this Twig apply_filter extension here:

github.com/marcj/twig-apply_filter-bundle

Or via packagist:

packagist.org/packages/marcj/twig-apply_filter-bundle

Example


{{ set filters = 'upper|nl2br' }}

{{ value|apply_filter(filters) }}

{{ value|apply_filter("default('abc')|json_encode") }}

A filter for e.g. a News system, where the title filter is stored in the database:



{% for item in newsItems %}
    <h2>{{ item.title|apply_filter(databaseSettings.newsFilter) }}</h2>
{% endfor %}