php Twig 中的“While”和“repeat”循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27691672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"While" and "repeat" loops in Twig
提问by Aistis
Are there any nice ways to use while and repeat loops in Twig? It is such a simple task, but without macros I can't find anything nice and simple.
有什么好的方法可以在 Twig 中使用 while 和 repeat 循环吗?这是一项如此简单的任务,但没有宏,我找不到任何好的和简单的东西。
At least do an infinite cycle and then break it in a condition?
至少做一个无限循环,然后在一个条件下打破它?
EDIT:
编辑:
I mean something like
我的意思是像
do {
// loop code
} while (condition)
or
或者
while (condition) {
// loop code
}
Edit 2:
编辑2:
Looks like it is not supported natively by twig same reason as it is not supported neither continue;
or break;
statements.
像它看起来是因为它不支持既没有原生枝条同样的理由支持continue;
或break;
语句。
采纳答案by Niels Keurentjes
In a nutshell: no. This functionality implies advanced logic, which should be in your business logic, not in the template layer. It's a prime example of the separation of concerns in MVC.
简而言之:没有。此功能意味着高级逻辑,它应该在您的业务逻辑中,而不是在模板层中。这是 MVC 中关注点分离的一个主要例子。
Twig supports for
-loopscompletely, which should suffice if you code correctly - being that complex conditional decisions on which data to display are taken in the business logic where they belong, which then pass a resulting array 'ready to render' to the templates. Twig then supports all nice features only needed for rendering.
Twig完全支持for
-loops,如果您正确编码,这应该就足够了 - 在它们所属的业务逻辑中对要显示的数据进行复杂的条件决定,然后将结果数组“准备呈现”传递给模板。然后 Twig 支持渲染所需的所有优秀功能。
回答by fregante
You can emulate it with for ... in ... if
by using a sufficiently-high loop limit (10000?)
您可以for ... in ... if
通过使用足够高的循环限制(10000?)来模拟它
while
尽管
PHP:
PHP:
$precondition = true;
while ($precondition) {
$precondition = false;
}
Twig:
枝条:
{% set precondition = true %}
{% for i in 0..10000 if precondition %}
{% set precondition = false %}
{% endfor %}
do while
做一会儿
PHP:
PHP:
do {
$condition = false;
} while ($condition)
Twig:
枝条:
{% set condition = true %} {# you still need this to enter the loop#}
{% for i in 0..10000 if condition %}
{% set condition = false %}
{% endfor %}
回答by DslDip
I was able to implement a simple for loop in twig. So the following php statement:
我能够在树枝中实现一个简单的 for 循环。所以下面的php语句:
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
when translated to twig is:
当翻译成树枝时是:
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
It's not a while loop but a potential workaround. The best suggestion is to leave business logic like this out of the template layer.
这不是一个 while 循环,而是一个潜在的解决方法。最好的建议是将这样的业务逻辑留在模板层之外。
回答by Xesau
This is possible, but a little bit complicated.
这是可能的,但有点复杂。
You can use {% include ... %}
to process nested arrays, which from the comments I read is what you need to do.
您可以使用{% include ... %}
来处理嵌套数组,从我阅读的评论中可以看出这是您需要做的。
Consider the following code:
考虑以下代码:
nested_array_display.html
nested_array_display.html
<ul>
{% for key, val in arr %}
<li>
{{ key }}:
{% if val is iterable %}
{% include 'nested_array_display.html' %}
{% else %}
{{ val }}
{% endif %}
</li>
{% endfor %}
</ul>