Twig for loop for arrays with keys

前端 未结 4 1577
故里飘歌
故里飘歌 2020-11-30 20:01

I use Twig and I have an array with keys like this:

array[1] = \"alpha\"
array[2] = \"bravo\"
array[3] = \"charlie\"
array[8] = \"delta\"
array[9] = \"echo\"         


        
相关标签:
4条回答
  • 2020-11-30 20:31

    I found the answer :

    {% for key,value in array_path %}
        Key : {{ key }}
        Value : {{ value }}
    {% endfor %}
    
    0 讨论(0)
  • 2020-11-30 20:31

    There's this example in the SensioLab page on the for tag:

    <h1>Members</h1>
    <ul>
        {% for key, user in users %}
            <li>{{ key }}: {{ user.username|e }}</li>
        {% endfor %}
    </ul>
    

    http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys

    0 讨论(0)
  • 2020-11-30 20:31

    These are extended operations (e.g., sort, reverse) for one dimensional and two dimensional arrays in Twig framework:

    1D Array

    Without Key Sort and Reverse

    {% for key, value in array_one_dimension %}
        <div>{{ key }}</div>
        <div>{{ value }}</div>
    {% endfor %}
    

    Key Sort

    {% for key, value in array_one_dimension|keys|sort %}
        <div>{{ key }}</div>
        <div>{{ value }}</div>
    {% endfor %}
    

    Key Sort and Reverse

    {% for key, value in array_one_dimension|keys|sort|reverse %}
        <div>{{ key }}</div>
        <div>{{ value }}</div>
    {% endfor %}
    

    2D Arrays

    Without Key Sort and Reverse

    {% for key_a, value_a in array_two_dimension %}
        {% for key_b, value_b in array_two_dimension[key_a] %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    

    Key Sort on Outer Array

    {% for key_a, value_a in array_two_dimension|keys|sort %}
        {% for key_b, value_b in array_two_dimension[key_a] %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    

    Key Sort on Both Outer and Inner Arrays

    {% for key_a, value_a in array_two_dimension|keys|sort %}
        {% for key_b, value_b in array_two_dimension[key_a]|keys|sort %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    

    Key Sort on Outer Array & Key Sort and Reverse on Inner Array

    {% for key_a, value_a in array_two_dimension|keys|sort %}
        {% for key_b, value_b in array_two_dimension[key_a]|keys|sort|reverse %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    

    Key Sort and Reverse on Outer Array & Key Sort on Inner Array

    {% for key_a, value_a in array_two_dimension|keys|sort|reverse %}
        {% for key_b, value_b in array_two_dimension[key_a]|keys|sort %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    

    Key Sort and Reverse on Both Outer and Inner Array

    {% for key_a, value_a in array_two_dimension|keys|sort|reverse %}
        {% for key_b, value_b in array_two_dimension[key_a]|keys|sort|reverse %}
            <div>{{ key_b }}</div>
            <div>{{ value_b }}</div>
        {% endfor %}
    {% endfor %}
    
    0 讨论(0)
  • 2020-11-30 20:45

    I guess you want to do the "Iterating over Keys and Values"

    As the doc here says, just add "|keys" in the variable you want and it will magically happen.

    {% for key, user in users %}
        <li>{{ key }}: {{ user.username|e }}</li>
    {% endfor %}
    

    It never hurts to search before asking :)

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