我想优化我的源代码 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/