我如何在树枝中实现有效的 if 语句?我的不工作
How do i implement a working if Statement in twig? mine isnt working
我正在尝试在使用 twig 进行模板化时执行一个简单的 IF 语句。似乎它被忽略了,每次都会读取以下代码。应该很简单,但就是行不通。
{% extends "_layout" %}
{% block content %}
<div class="box">
{% set x = 1 %}
{% for entry in craft.entries.section('news') %}
{% if x % 3 == 0 %}
<div class="row">
</div>
{% endif %}
{% set x =+ 1 %}
<div class="beitrag col l4 m12 s12">
<h3 class="beitrag_titel">{{ entry.title }}</h3> <p>{{ entry.textnews }}</p>
</div>
{% endfor %}
</div>
{% endblock %}
我希望代码每三个条目执行一个新的 "row" div。他所做的就是每次都做一个新的。
感谢答案
您对 x
的分配是错误的。 twig
.
中没有后增量
此时你只需每次将 x
设置为 +1
即可。你应该使用 {% set x = x + 1 %}
一些注意事项,不必在 for 循环中跟踪额外的计数器,因为您可以使用 loop.index
或 loop.index0
,分别从 1 和 0 开始。
您也可以使用测试 is divisible by
来提高可读性
{% for i in 0..10 %}
{% if loop.index is divisible by(3) %}
{{ loop.index }} is divisible by 3
{% endif %}
{% endfor %}
要解决 html 行问题,您还可以使用 batch
<div class="box">
{% for row in data|batch(3) %}
{% for value in row %}
<div class="beitrag col l4 m12 s12">
{{ value }}
</div>
{% endfor %}
<div class="row"></div>
{% endfor %}
</div>
根据你(已删除)的评论,如果你想让内容进入 row
,你需要按以下方式调整代码
<div class="box">
{% set x = 1 %}
{% for entry in entries %}
{% if loop.first %}
<div class="row">
{% endif %}
<div class="col l4 m12 s12">
<h3 class="beitrag_titel">{{ entry.title }}</h3>
<p>{{ entry.textnews }}</p>
</div>
{% if x is divisible by(3) and not loop.last %}
</div>
<div class="row">
{% endif %}
{% set x = x + 1 %}
{% if loop.last and entries|length is not divisible by(3) %}
</div>
{% endif %}
{% endfor %}
</div>
但是你最好使用批处理
<div class="box">
{% for row in entries|batch(3) %}
<div class="row">
{% for value in row %}
<div class="beitrag col l4 m12 s12">
<h3>{{ value.title }}</h3>
<p>{{ value.textnews }}</p>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
我正在尝试在使用 twig 进行模板化时执行一个简单的 IF 语句。似乎它被忽略了,每次都会读取以下代码。应该很简单,但就是行不通。
{% extends "_layout" %}
{% block content %}
<div class="box">
{% set x = 1 %}
{% for entry in craft.entries.section('news') %}
{% if x % 3 == 0 %}
<div class="row">
</div>
{% endif %}
{% set x =+ 1 %}
<div class="beitrag col l4 m12 s12">
<h3 class="beitrag_titel">{{ entry.title }}</h3> <p>{{ entry.textnews }}</p>
</div>
{% endfor %}
</div>
{% endblock %}
我希望代码每三个条目执行一个新的 "row" div。他所做的就是每次都做一个新的。
感谢答案
您对 x
的分配是错误的。 twig
.
中没有后增量
此时你只需每次将 x
设置为 +1
即可。你应该使用 {% set x = x + 1 %}
一些注意事项,不必在 for 循环中跟踪额外的计数器,因为您可以使用 loop.index
或 loop.index0
,分别从 1 和 0 开始。
您也可以使用测试 is divisible by
来提高可读性
{% for i in 0..10 %}
{% if loop.index is divisible by(3) %}
{{ loop.index }} is divisible by 3
{% endif %}
{% endfor %}
要解决 html 行问题,您还可以使用 batch
<div class="box">
{% for row in data|batch(3) %}
{% for value in row %}
<div class="beitrag col l4 m12 s12">
{{ value }}
</div>
{% endfor %}
<div class="row"></div>
{% endfor %}
</div>
根据你(已删除)的评论,如果你想让内容进入 row
,你需要按以下方式调整代码
<div class="box">
{% set x = 1 %}
{% for entry in entries %}
{% if loop.first %}
<div class="row">
{% endif %}
<div class="col l4 m12 s12">
<h3 class="beitrag_titel">{{ entry.title }}</h3>
<p>{{ entry.textnews }}</p>
</div>
{% if x is divisible by(3) and not loop.last %}
</div>
<div class="row">
{% endif %}
{% set x = x + 1 %}
{% if loop.last and entries|length is not divisible by(3) %}
</div>
{% endif %}
{% endfor %}
</div>
但是你最好使用批处理
<div class="box">
{% for row in entries|batch(3) %}
<div class="row">
{% for value in row %}
<div class="beitrag col l4 m12 s12">
<h3>{{ value.title }}</h3>
<p>{{ value.textnews }}</p>
</div>
{% endfor %}
</div>
{% endfor %}
</div>