symfony - 使用 Twig 优化代码

标签 symfony twig

我想优化我的源代码 Twig,但这对我来说看起来不太有效。

我的代码:

{% for info in infos %}
  {% set i = 0 %}
  {% if loop.index0 is divisibleby(3) %}

    <div class="row-fluid">
    <ul class="thumbnails">

    {% set open = 'yes' %} 

  {% endif %}

  <li class="span4">
    <div class="thumbnail">
      <img src="#" alt="">
      <div class="caption">
        <h4>{{ info.titre }}</h4>
        <p>{{ info.text }}</p>
        <p><a href="#" class="btn btn-primary">{# a traduire multi #}Lire</a></p>
      </div>
    </div>
  </li>

  {% if loop.index is divisibleby(3) and open == 'yes' %}
    </ul>

    </div>

  {% endif %}

{% endfor %} 

如何避免创建如此多的变量并使用相同的条件两次?

最佳答案

也许以下内容不起作用,但我希望您能明白:

<div class="row-fluid">
    <ul class="thumbnails">
        {% for info in infos %}
            {% if loop.index0 is divisibleby(3) and loop.index0 != 0 %}
    </ul>
</div>
<div class="row-fluid">
    <ul class="thumbnails">
            {% endif %}

        <li class="span4">
            <div class="thumbnail">
                <img src="#" alt="" />
                <div class="caption">
                    <h4>{{ info.titre }}</h4>
                    <p>{{ info.text }}</p>
                    <p><a href="#" class="btn btn-primary">{# a traduire multi #}Lire</a></p>
                </div>
            </div>
       </li>
      {% endfor %}
    </ul>
</div>

关于symfony - 使用 Twig 优化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11525878/

相关文章:

symfony - FOSElasticaBundle 未填充 AWS ElasticSearch 中的 searchableDocuments

php - 在 symfony 中插入\ double 值

php - 带有 Symfony2 的购物车 bundle

html - 将 novalidate 属性包含到 Symfony2 twig 模板中

twig - 在twig中的另一个循环中访问loop.index

javascript - 主干模型已保存,但向 HTTP 发送空数组

symfony - 将cookie传递给CloudFront来源,但阻止缓存

twig - 如何将变量添加到自定义 Twig 的 TokenParser?

javascript - Twig JS (gulp-twig) - 牢记 DRY 原则遍历大型数据结构

php - laravel - php artisan 迁移失败