Twig sort array of objects by field

后端 未结 8 2106
感动是毒
感动是毒 2020-12-17 09:31

I have entity with fields:

User

  • name
  • lastname
  • age

and few more. Im sending to Twig array with objec

相关标签:
8条回答
  • 2020-12-17 09:42

    For those working with Collections in Grav, you can simply do this:

    {% for user in users.order("name", "asc") %}
         ...
    {% endfor %}
    

    Of course, name can be any property of the given object and asc can alternatively be desc.

    This also works with dot-concatenated properties:

    {% for child in page.children.order("page.headers.foo.bar", "desc") %}
    
    0 讨论(0)
  • 2020-12-17 09:43

    Yes, You can add custom filter:

    {% for user in users|usort %}
        ...
    {% endfor %}
    

    and then add Extension/new filter to Twig:

    new \Twig_SimpleFilter('usort', array($this, 'usortFilter'))

    public function usortFilter($item){
        usort($item, function ($item1, $item2) {
            if ($item1['orderNo'] == $item2['orderNo']) return 0;
            return $item1['orderNo'] < $item2['orderNo'] ? -1 : 1;
        });
    
        return $item;
    }
    
    0 讨论(0)
  • 2020-12-17 09:44

    It's not possible in twig out of the box to order by specific field. You can sort by id using PHP asort in twig that would be the |sort filter, or you could write a custom twig extension which does what you need.

    {% for user in users|sort %}
        ...
    {% endfor %}
    

    and revers the order using |reverse filter

    {% for user in users|reverse(true) %}
        ...
    {% endfor %}
    
    0 讨论(0)
  • 2020-12-17 09:46

    With new version twig you can sort by colonne

    {% set fruits = [
    { name: 'Apples', quantity: 5 },
    { name: 'Oranges', quantity: 2 },
    { name: 'Grapes', quantity: 4 },] %}
    {% for fruit in fruits|sort((a, b) => a.quantity <=> b.quantity)|column('name') %}
        {{ fruit }}
    {% endfor %}
    

    https://twig.symfony.com/doc/2.x/filters/sort.html

    0 讨论(0)
  • 2020-12-17 09:47

    I'm using Timber for Wordpress which still uses Twig 1+, so I couldn't use the arrow argument. I just created a filter like this using PHP’s usort function:

    // sort an array of objects by parameter
    $twig->addFilter( new Twig_SimpleFilter( 'sort_by_key', 
        function ( $array, $key, $order = 'asc', $type = 'string' ) {
            usort( $array, function($a, $b) use ($key, $order, $type) {
            $a = $a[$key]; $b = $b[$key];
            if( $type === 'date' ) {
                $a = strtotime( $a );
                $b = strtotime( $b );
            }
            if( $a == $b ) {
                return 0;
            }
            switch( $order ) {
                case 'desc' : case  'DESC' :
                    return $a < $b ?  1 : -1;
                    break;
                default :
                    return $a < $b ?  -1 : 1;
            }
            }  );
            return $array;
        }));
    

    And then in my twig template:

        {% set array = myArrayOfObjects | sort_by_key('date', 'desc', 'date') %}
    

    Works well with the Advanced Custom Fields repeater field.

    0 讨论(0)
  • 2020-12-17 09:49

    Starting with Twig 2.12 (released on October 5, 2019) you can use the sort filter with an arrow function in the arrow argument.

    For example, to order by name:

    {% for user in users|sort((a, b) => a.name <=> b.name) %}
        <td>{{ user.name }}</td> <td>{{ user.lastname}}</td> <td>{{ user.age}}</td>
    {% endfor %}
    

    Twig docs: https://twig.symfony.com/doc/2.x/filters/sort.html

    0 讨论(0)
提交回复
热议问题