递增声明的整型变量
我尝试做斑马条纹:
{% set counter = 0 %}
{% for entity in entities %}
<tr class="{{ cycle(['odd', 'even'], counter) }}">
{% counter++ %}
但出现错误:
意外的标记名称“counter”(需要“for”标记的结束标记 定义在第 11 行附近)
有人能给我解决方案吗?
[编辑]
我的糟糕解决方案非常简单:
{% set counter = counter + 1 %}
I try to do zebra striping:
{% set counter = 0 %}
{% for entity in entities %}
<tr class="{{ cycle(['odd', 'even'], counter) }}">
{% counter++ %}
but I am getting error:
Unexpected tag name "counter" (expecting closing tag for the "for" tag
defined near line 11)
Could somebody give me solution?
[EDIT]
My bad solution is so easy:
{% set counter = counter + 1 %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种更简单的方法可以完成您想要的操作:
请参阅文档 用于
循环
好东西。There's an easier way to do what you want:
See the docs for the
loop
goodies.如果你想完全控制html,你可以试试这个:
你可以在这里阅读:http://twig.sensiolabs.org/doc/tests/divisibleby.html
请注意,
loop.index
按“原样”使用,它并不引用变量,而是隐藏索引for 循环的。If you want to have full control over the html, you can try this:
You can read it here: http://twig.sensiolabs.org/doc/tests/divisibleby.html
note that
loop.index
is used 'as-is', it does not refer to a variable rather the hidden indexing of the for loop.