“虽然"和“重复"树枝中的循环 [英] "While" and "repeat" loops in Twig

查看:100
本文介绍了“虽然"和“重复"树枝中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Twig中,有没有什么好方法可以使用while和重复循环?这是一个非常简单的任务,但是没有宏,我找不到任何简单易用的东西.

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.

至少要做一个无限循环然后在一定条件下将其破坏?

At least do an infinite cycle and then break it in a condition?

我的意思是

do {
    // loop code
} while (condition)

while (condition) {
    // loop code
}

看起来像树枝一样不支持它,原因同样是continue;break;语句都不受支持.

Looks like it is not supported natively by twig same reason as it is not supported neither continue; or break; statements.

https://github.com/twigphp/Twig/issues/654

推荐答案

简而言之:不.此功能暗示了高级逻辑,该逻辑应该在您的业务逻辑中,而不是模板层中.这是MVC中关注点分离的一个很好的例子.

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.

Twig完全支持 for-循环,如果您正确编写代码就足够了-就是那样有关要显示哪些数据的复杂条件决定是在它们所属的业务逻辑中做出的,然后将结果数组准备渲染"传递给模板.然后Twig支持仅渲染所需的所有不错的功能.

Twig supports for-loops completely, 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.

这篇关于“虽然"和“重复"树枝中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆