jmf / twig-sort
Twig extension for sorting arrays in Twig templates.
1.0.0
2024-06-05 01:00 UTC
Requires (Dev)
- phing/phing: ^2.17
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.1
- rector/rector: ^1.0
- squizlabs/php_codesniffer: ^3.8
README
Installation & Requirements
Install with Composer:
composer require jmf/twig-sort
Usage in Twig templates
sort() filter
Sorts provided array by value, losing key-value association.
{% set sorted = values|sort %}
rsort() filter
Reverse-sorts provided array by value, losing key-value association.
{% set sorted = values|rsort %}
asort() filter
Sorts provided array by value, preserving key-value association.
{% set sorted = values|asort %}
arsort() filter
Reverse-sorts provided array by value, preserving key-value association.
{% set sorted = values|arsort %}
ksort() filter
Sorts provided array by key.
{% set sorted = values|ksort %}
krsort() filter
Reverse-sorts provided array by key.
{% set sorted = values|krsort %}
psort() filter
Sorts provided array of arrays or array of objects by properties.
{% set sorted = articles|psort('title') %} {% set sorted = articles|psort(['title', 'author.name']) %} {% set sorted = articles|psort({'publication_date': 'desc', 'author': 'asc'}) %}