php Twig,添加第一个和最后一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7837482/
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
Twig, add first and last class
提问by FMaz008
Let's say I have a for loop like this:
假设我有一个这样的 for 循环:
{% for elem in arrMenu %}
<div class="topmenu-button">
<a href="{{ elem.url }}">{{ elem.name }}</a>
</div>
{% endfor %}
In that form, it would render something like:
以这种形式,它会呈现如下内容:
<div class="topmenu-button"><a href="url">name</a></div>
<div class="topmenu-button"><a href="url">name</a></div>
<div class="topmenu-button"><a href="url">name</a></div>
<div class="topmenu-button"><a href="url">name</a></div>
How can twig help me to add first and last classes the the div, so that I would have a result like:
twig 如何帮助我在 div 中添加第一个和最后一个类,以便我得到如下结果:
<div class="topmenu-button first"><a href="url">name</a></div>
<div class="topmenu-button"><a href="url">name</a></div>
<div class="topmenu-button"><a href="url">name</a></div>
<div class="topmenu-button last"><a href="url">name</a></div>
回答by Kendall Hopkins
You can use the "special variables" loop.first
and loop.last
for what you want.
您可以使用“特殊变量”loop.first
和loop.last
您想要的。
{% for elem in arrMenu %}
{% if loop.first %}
<div class="topmenu-button first">
{% elseif loop.last %}
<div class="topmenu-button last">
{% else %}
<div class="topmenu-button">
{% endif %}
<a href="{{ elem.url }}">{{ elem.name }}</a>
</div>
{% endfor %}
EDIT: depending how much you care about the "one case", you might need a solution like this.
编辑:取决于您对“一个案例”的关心程度,您可能需要这样的解决方案。
{% for elem in arrMenu %}
{% set classes = ["topmenu-button"] %}
{% if loop.first %}{% set classes = classes|merge(["first"]) %}{% endif %}
{% if loop.last %}{% set classes = classes|merge(["last"]) %}{% endif %}
<div class="{{ classes|join(" ") }}">
<a href="{{ elem.url }}">{{ elem.name }}</a>
</div>
{% endfor %}
回答by gremo
Since a loop can't be first
and last
at the same time, i would prefer not to use elseif
and write (DRY - what if you have to change topmenu-button
in future?):
由于循环不能first
与last
在同一时间,我宁愿不使用elseif
和写(DRY -如果你要改变topmenu-button
未来?):
{% for elem in arrMenu %}
{% set append %}
{% if loop.first %}first{% endif %}
{% if loop.last %}last{% endif %}
{% endset %}
<div class="topmenu-button {{ append }}">
<a href="{{ elem.url }}">{{ elem.name }}</a>
</div>
{% endfor %}
or more concise:
或更简洁:
{% for elem in arrMenu %}
<div class="topmenu-button {% if loop.first %}first{% endif %} {% if loop.last %}last{% endif %}">
<a href="{{ elem.url }}">{{ elem.name }}</a>
</div>
{% endfor %}
Edit: this way you'll get some extra spaces in class
attribute (it's perfectly fine btw).
Edit2: this will not handle 1-element array (first = last)
编辑:这样你会在class
属性中得到一些额外的空格(顺便说一句,它非常好)。
Edit2:这不会处理 1 元素数组(第一个 = 最后一个)
回答by Paul Leclerc
If it's to manage class attribut of a tag, a ternary condition would be better.
如果是管理标签的类属性,三元条件会更好。
<span class="topmenu-button {{ (loop.first) ? 'first' : '' }}">{{ item.text }}</span>
回答by Gódi Péter
I use this in Drupal 8 views template. Thanks for 'elseif', if you have only 1 item, then the first item never get the 'last' class:
我在 Drupal 8 视图模板中使用它。感谢“elseif”,如果您只有一项,那么第一项永远不会获得“最后”类:
{% for row in rows %}
{% set parity = cycle(['odd', 'even'], loop.index0) %}
{% set row_classes = [ default_row_class ? 'views-row', ] %}
<div class="views-prerow {{parity}}{% if loop.first %} first{% elseif loop.last %} last{% endif %}">
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
</div>
{% endfor %}