All Projects → dpolac → Twig Lambda

dpolac / Twig Lambda

Licence: mit
Lambda expressions for Twig and filters that make use of them

Projects that are alternatives of or similar to Twig Lambda

Shuffle
Categorize, sort, and filter a responsive grid of items
Stars: ✭ 2,170 (+5325%)
Mutual labels:  sort, filter
spring-boot-jpa-rest-demo-filter-paging-sorting
Spring Boot Data JPA with Filter, Pagination and Sorting
Stars: ✭ 70 (+75%)
Mutual labels:  filter, sort
Sortfilterproxymodel
A nicely exposed QSortFilterProxyModel for QML
Stars: ✭ 214 (+435%)
Mutual labels:  sort, filter
Faltu
Search sort, filter, limit an array of objects in Mongo-style.
Stars: ✭ 112 (+180%)
Mutual labels:  sort, filter
Gridjs
Advanced table plugin
Stars: ✭ 3,231 (+7977.5%)
Mutual labels:  sort, filter
Ng2 Smart Table
Angular Smart Data Table component
Stars: ✭ 1,590 (+3875%)
Mutual labels:  sort, filter
DrupalTwigFood
Useful functions, filters for twig @ Drupal 8
Stars: ✭ 1 (-97.5%)
Mutual labels:  twig, filter
repository
[PHP 7] Implementation and definition of a base Repository in Domain land.
Stars: ✭ 26 (-35%)
Mutual labels:  filter, sort
Graphql To Mongodb
Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries
Stars: ✭ 261 (+552.5%)
Mutual labels:  sort, filter
Jschema
A simple, easy to use data modeling framework for JavaScript
Stars: ✭ 261 (+552.5%)
Mutual labels:  sort, filter
Muuri
Infinite responsive, sortable, filterable and draggable layouts
Stars: ✭ 9,797 (+24392.5%)
Mutual labels:  sort, filter
Filterizr
✨ Filterizr is a JavaScript library that sorts, shuffles and filters responsive galleries using CSS3 transitions ✨
Stars: ✭ 546 (+1265%)
Mutual labels:  sort, filter
Queryql
Easily add filtering, sorting, and pagination to your Node.js REST API through your old friend: the query string!
Stars: ✭ 76 (+90%)
Mutual labels:  sort, filter
Laravel Api Handler
Package providing helper functions for a Laravel REST-API
Stars: ✭ 150 (+275%)
Mutual labels:  sort, filter
Htmlpurifierbundle
HTML Purifier is a standards-compliant HTML filter library written in PHP.
Stars: ✭ 234 (+485%)
Mutual labels:  twig, filter
express-mquery
Expose mongoose query API through HTTP request.
Stars: ✭ 37 (-7.5%)
Mutual labels:  filter, sort
Mixitup
A high-performance, dependency-free library for animated filtering, sorting, insertion, removal and more
Stars: ✭ 4,431 (+10977.5%)
Mutual labels:  sort, filter
Sieve
⚗️ Clean & extensible Sorting, Filtering, and Pagination for ASP.NET Core
Stars: ✭ 560 (+1300%)
Mutual labels:  sort, filter
Metalpetal
A GPU accelerated image and video processing framework built on Metal.
Stars: ✭ 907 (+2167.5%)
Mutual labels:  filter
Amptwigtheme
AMP oriented theme for Twig template engine
Stars: ✭ 33 (-17.5%)
Mutual labels:  twig

Twig Lambda

Lambda expressions for Twig and filters that make use of them


Quick examples

Listing names of all authors ordered by age:

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

Counting elements starting from specified letter:

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

Installation

Install via Composer:

composer require dpolac/twig-lambda

Add the extension to Twig:

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

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

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

Usage

Lambda expression

To create lambda expression prepend any valid Twig expression with => operator. Inside of the lambda expression you can use any variable from the outside. There are also two special variables available:

  • _ (single underscore) - first argument,
  • __ (double underscore) - array of arguments counted from zero.
=> _.name
=> _ * 2
=> _|first
=> 'foobar'
=> _ is even
=> __[0] + __[1]

To create lambda expression with list of arguments, add it before => operator. Separate multiple arguments with semicolons. You can use brackets for readability.

x => x + 1
(book) => book.author
arg1; arg2 => arg1 ~ arg2
(a; b; c) => a + b - c

Note that if you use list of arguments, _ variable is not longer available.


Below is a list of available filters and tests. All works with arrays and any Traversable object and preserve it keys. All lambdas are called with two arguments: element and key.


|map

Alias: |select
Signature: array|map(lambda)

Applies a given function to each element and returns array of results in the same order.

{% for i in  [1, 2, 3, 4]|map(=> _ * 2) %}
    {{ i }} {# prints '2 4 6 8' #}
{% endfor %}

|filter

Alias: |where
Signature: array|filter(lambda)

Returns array of elements that passes a test specified by lambda.

{% for i in [1, 2, 3, 4, 5, 6]|filter(=> _ is even) %}
    {{ i }} {# prints '2 4 6' #}
{% endfor %}

|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(=> _|first|capitalize) %}
    = {{ key }}
    {% for i in group %}
        * {{ i }}
    {% endfor %}
{% endfor %}

will produce

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

|sort_by

Signature: array|sort_by(lambda[, direction = 'ASC'])

Sorts array by values returned by lambda. Direction can be 'ASC' or 'DESC'.

{% for i in ['bar', 'fo', 'foobar', 'foob']|sort_by(=> _|length, 'DESC') %}
    {{ i }} {# prints 'foobar foob bar fo' #}
{% endfor %}

|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(=> _|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(=> _ 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(=> _ > 0) ? "All elements in the array are positive." }}
{# prints 'All elements in the array are positive.' #}

call()

Signature: call(lambda [, arguments:array])

Calls lambda and returns its result. You can provide array of arguments.

This function is provided to allow creating twig macros taking lambda as an argument.

{{ call(=> _ * 2, [10]) }}
{# prints '20' #}
{{ call(=> _.foo, [{foo: 12}]) }}
{# prints '12' #}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].