简体   繁体   English

如何在树枝模板中创建循环?

[英]How create loop in twig template?

I want made loop like of this on twig template:我想在树枝模板上制作这样的循环:

for($i=1;$i<100;$i++) {
 echo $i;
}

I solved this problem as:我解决了这个问题:

{% if k > 0 %}
 {% for i in 0..k - 1 %}
      <div></div>
 {% endfor %}
{% endif %}

if k = 0 -> no loop
if k = 1 -> 1 loop
if k = 100 -> 100 lopp

May be is other solve?可能是其他解决办法?

You can use range to reproduce loop output similar to the first php example您可以使用range来重现类似于第一个 php 示例的循环输出

{% for i in range(1, 100-1) %}
    {{ i }}
{% endfor %}

You can add the if inside the loop您可以在loop内添加if

{% for i in 0..k if k > 0 %}
 {{ i }}
{% endfor %}

demo演示


edit: This doesn't work anymore in twig 3.X.编辑:这在树枝 3.X 中不再起作用。 You have to place the if inside the codeblock您必须将if放在代码块中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM