Setting element of array from Twig

前端 未结 10 742
北恋
北恋 2020-12-04 09:55

How can I set member of an already existing array from Twig?

I tried doing it next way:

{% set arr[\'element\'] = \'value\' %}

but

相关标签:
10条回答
  • 2020-12-04 10:42

    If initialization only need:

    {% set items = { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'unknown' } %}
    
    0 讨论(0)
  • 2020-12-04 10:44

    I ran into this problem but was trying to create integer indexes instead of associative index like 'element'.

    You need to protect your index key with () using the merge filter as well:

    {% set arr = arr|merge({ (loop.index0): 'value'}) %} 
    

    You can now add custom index key like ('element'~loop.index0)

    0 讨论(0)
  • 2020-12-04 10:46

    Just use this like {% set arr={'key':'value'} %} (with no blank space after the :), it works well.

    But when I use it inside a for loop, to make it an array, it does not work outside of the for scope.

    {% for group in user.groups %}
      {% set foo={'loop.index0':'group.id'} %}
      {% set title={'loop.index0':'group.title'} %}
      {{ title }} //it work 
    {% else %}
      {% set foo={'0':'-1'} %}
      {% set title={'0':'未分组'} %}
    {% endfor %}
    {{ title }}  //it does not work, saying title is not defined
    
    0 讨论(0)
  • 2020-12-04 10:47

    I had this problem sometime ago. Imagine you have an array like this one:

    data = {
        'user': 'admin',
        'password': 'admin1234',
        'role': 'admin',
        'group': 'root',
        'profile': 'admin',
        'control': 'all',
        'level': 1,
        'session': '#DFSFASADASD02',
        'pre_oa': 'PRE-OA',
        'hepa_oa': 'HEPA-OA',
        'pre_ra': 'HEPA-RA',
        'hepa_ra': 'HEPA-RA',
        'deodor_ra': 'DEODOR-RA'
    }
    

    So, you want to show this data in two rows, but remove the password from that list. To this end, split in 2 arrays will be easy with the slice filter. However, we have to remove the password. For that reason, I'm using this snippet. The idea is to put all the elements lesser than the data elements size divided by 2. To calculate this we use the filter length. Now to get the index of the current element we user loop.index. And finally we *push an associative element in the left or right array. An associative array has two components key and value. To reference an array key in twit we operator () and we use the merge filter to push into the array as shown here {% set left_list = left_list|merge({ (key): value }) %}

    This is the complete solution.

    {% set left_list = {} %}
    {% set right_list = {} %}
    {% set limit = data|length // 2 %}
    {% for key, value in data|cast_to_array %}
    {% if key != 'password' %}
    {% if loop.index <= limit %}
    {% set left_list = left_list|merge({ (key): value }) %}
    {% else %}
    {% set right_list = right_list|merge({ (key): value }) %}
    {% endif %}
    {% endif %}
    {% endfor %}
    {% for key, value in left_list %}
    <p>
    <label for="{{key}}">{{key}}</label>
    <input type="text" name="{{key}}" id="{{key}}" value="{{value}}"
           class="text ui-widget-content ui-corner-all">
    </p>
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题