【问题标题】:Loop through array in template with Twig使用 Twig 循环遍历模板中的数组
【发布时间】:2023-03-20 00:49:01
【问题描述】:

我有这个数组

{% set icons = { 1: 'glyphicon glyphicon-asterisk', 
                 2: 'glyphicon glyphicon-plus', 
                 3: 'glyphicon glyphicon-minus',
                 4: 'glyphicon glyphicon-euro'   
                 } 
%}

我想遍历它并像这样打印

<li{{ item.attributes.addClass('pos' ~ loop.index, icons[loop.index]) }}>

【问题讨论】:

    标签: twig drupal-8


    【解决方案1】:

    像这样:

    {% for key, value in icons %}
       <li{{ item.attributes.addClass('pos' ~ key, value) }}>
    {% endfor %}
    

    另见http://twig.sensiolabs.org/doc/tags/for.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多