Skip to content

Lambda expressions for Twig and filters that make use of them

License

Notifications You must be signed in to change notification settings

leonaero/twig-lambda

 
 

Repository files navigation

Twig Lambda


Version Twig Version Php Version
^3.0 ^3.0 ^7.2 || ^8.0
^2.0 ^2.10 ^7.0
^1.0 ^1.0 || 2.9.* ^5.6 || ^7.0

Quick examples

Listing names of all authors ordered by youngest:

{% set articles = [
	{author: {name: 'Bar', age: 55}, text: 'Text...'},
	{author: {name: 'Bar', age: 65}, text: 'Text...'},
	{author: {name: 'Foo', age: 45}, text: 'Text...'},
	{author: {name: 'Foo', age: 45}, text: 'Text...'},
] %}

{% for author in articles|map(v => v.author)|unique_by('===')|sort(v => v.age)|reverse %}
    * {{ author.name }}, {{ author.age }}
{% endfor %}

Counting elements starting from specified letter:

{% for key, count in ['foo', 'bar', 'foobar']|count_by(v => v|first|capitalize) %}
    * {{ count }} elements start from {{ key }}.
{% endfor %}

Installation

Install via Composer:

composer require leonaero/twig-lambda

Add the extension to Twig:

$twig->addExtension(new \LeonAero\TwigLambda\LambdaExtension());

... or if you use Symfony, add the following to your services.yml config file:

services:
    # ...
    leonaero.twig_lambda.extension:
        class: LeonAero\TwigLambda\LambdaExtension
        tags: [ { name: twig.extension } ]

Usage

Below is a list of available filters. All works with arrays and any Traversable object and preserve it keys.


|unique_by

Signature: array|unique_by(lambda|'==='|'==')

Returns array of unique elements. Uniqueness is checked with passed lambda. PHP operators == or === will be used if string '==' or '===' is passed instead of lambda.

Lambda should have two arguments - items to check. Keys of elements are also passed as third and fourth argument.

    {% for i in [1, 2, 2, 3, 1]|unique_by((i1, i2) => i1 == i2) %}
        {{ i }} {# prints '1 2 3' #}
    {% endfor %}

equivalent

    {% for i in [1, 2, 2, 3, 1]|unique_by('==') %}
        {{ i }} {# prints '1 2 3' #}
    {% endfor %}

|group_by

Signature: array|group_by(lambda)

Sorts an array into groups by the result of lambda.

{% for key, group in ['foo', 'bar', 'foobar', 'barbar']|group_by(v => v|first|capitalize) %}
    = {{ key }}
    {% for i in group %}
        * {{ i }}
    {% endfor %}
{% endfor %}

will produce

    = F
        * foo
        * foobar
    = B
        * bar
        * barbar

|count_by

Signature: array|count_by(lambda)

Sorts an array into groups and returns a count for the number of objects in each group.

If lambda returns true, false or null, it will be converted to string 'true', 'false' or 'null'. Float will be converted to integer.

{% for key, count in ['foo', 'bar', 'foobar']|count_by(v => v|first|capitalize) %}
    * {{ count }} elements start from {{ key }}.
{% endfor %}

will produce

    * 2 elements start from F
    * 1 elements start from B

|is_any

Signature: array|is_any(lambda)

Returns true if lambda returns true for any element from an array.

Returns false if array is empty.

{{ [1, 2, 3]|is_any(v => v is even) ? "There is even element in the array." }}
{# prints 'There is even element in the array.' #}

|is_every

Signature: array|is_every(lambda)

Returns true if lambda returns true for every element from an array.

Returns true if array is empty.

{{ [1, 2, 3]|is_every(v => v > 0) ? "All elements in the array are positive." }}
{# prints 'All elements in the array are positive.' #}

About

Lambda expressions for Twig and filters that make use of them

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 97.5%
  • Dockerfile 2.1%
  • Shell 0.4%