简体   繁体   English

Twig - 如何循环特定次数

[英]Twig - How to loop particular number of times

I need to be able to generate the links certain number of times (stored in int variable)Is there a way to do it out of the box with twig's for loop? 我需要能够生成一定次数的链接(存储在int变量中)有没有办法用twig的for循环开箱即用?

{% for i in numberOfLoops %}
    {{ i }}. Some data
{% endfor %}

The above example do not work. 上面的例子不起作用。 I googled it but did not find actual solution. 我用谷歌搜索,但没有找到实际的解决方案。 Any support would be very appreciated. 任何支持将非常感谢。

EDIT: I also tried: 编辑:我也尝试过:

{% set k = 10 %}
{% for i in 0..k %}
    {{ i }}
{% endfor %}

but this generates an exception: 但这会产生一个例外:

com.lyncode.jtwig.exception.ParseException: Wrong binary operation syntax
Explanation: Input position (line 15, pos 27):
        {% for i in 0..k %}
                      ^

I found the working example: 我找到了工作示例:

{% set k = 10 %}
{% for i in range(1, k) %}
    {{ i }}
{% endfor %}

Source: http://twig.sensiolabs.org/doc/templates.html (not very intuitive to find indeed). 来源: http//twig.sensiolabs.org/doc/templates.html (确实不太直观)。

I already had a loop in place to iterate over, I solved this for myself with the slice filter. 我已经有一个循环来迭代,我用切片过滤器为自己解决了这个问题。

{% for link in links|slice(0, 12) %}

http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-a-subset http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-a-subset

Try this: 试试这个:

{% set k = 10 %}
{% for i in 0..k %}
    {{ i }}
{% endfor %}

Documentation: http://twig.sensiolabs.org/doc/tags/for.html 文档: http//twig.sensiolabs.org/doc/tags/for.html

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

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