Download the PHP package balint777/twig-lambda without Composer
On this page you can find all versions of the php package balint777/twig-lambda. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package twig-lambda
Twig Lambda
Lambda expressions for Twig and filters that make use of them
Quick examples
Listing names of all authors ordered by age:
Counting elements starting from specified letter:
Installation
Install via Composer:
Add the extension to Twig:
... or if you use Symfony, add the following to your services.yml
config file:
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.
To create lambda expression with list of arguments, add it
before =>
operator. Separate multiple arguments with
semicolons. You can use brackets for readability.
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.
|filter
Alias: |where
Signature: array|filter(lambda)
Returns array of elements that passes a test specified by lambda.
|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.
equivalent
|group_by
Signature: array|group_by(lambda)
Sorts an array into groups by the result of lambda.
will produce
|sort_by
Signature: array|sort_by(lambda[, direction = 'ASC'])
Sorts array by values returned by lambda. Direction can be 'ASC' or 'DESC'.
|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.
will produce
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.
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.
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.
All versions of twig-lambda with dependencies
twig/twig Version ^1.0 || ^2.0 || ^3.0
dpolac/dictionary Version ^1.0