递增声明的整型变量

发布于 2024-12-28 19:58:44 字数 402 浏览 2 评论 0原文

我尝试做斑马条纹:

{% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2025-01-04 19:58:44

有一种更简单的方法可以完成您想要的操作:

{{ cycle(["even", "odd"], loop.index) }}

请参阅文档 用于循环好东西。

There's an easier way to do what you want:

{{ cycle(["even", "odd"], loop.index) }}

See the docs for the loop goodies.

回忆凄美了谁 2025-01-04 19:58:44

如果你想完全控制html,你可以试试这个:

{% if loop.index is divisibleby(2) %}
    ...
{% endif %}

你可以在这里阅读: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:

{% if loop.index is divisibleby(2) %}
    ...
{% endif %}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文