我正在 Drupal 8 中使用分类术语模板页面,我需要能够仅根据填充页面内容的分类术语来显示特定内容。我正在使用taxonomy-term.twig.html template 作为起点,但我没有运气将字符串值与 {{ name }} 变量进行比较。这是我到目前为止的代码:
<section{{ attributes.addClass(classes)}}>
{% if 'general' in name %}
<img class="promo-image" src="http://placehold.it/150x150" alt="promo">
<h3 class="promo-title">Promo title text</h3>
<p class="promo-copy">lorem ipsum dolor set amet</p>
<div class="links">
<a href="/resources" class="btn outline-black-transparent">Learn more</a>
</div>
{% endif %}
</section>
如果我像正常的 {{ name }} 一样输出 name 变量,它会将标签名称打印到页面上,但我不知道如何将该值与任何内容进行比较。我也尝试过直接 if equals 路由,但似乎 name 变量是一个数组。
最佳答案
由于 name 变量是一个数组,我只需要添加的第一个值:
{%
set tag = name['#items'].0.value
%}
然后将 if 语句更改为:
{% if tag == 'text' %}
<section{{ attributes.addClass(classes)}}>
<img class="promo-image" src="http://placehold.it/150x150" alt="promo">
<h3 class="promo-title">Promo title text</h3>
<p class="promo-copy">lorem ipsum dolor set amet</p>
<div class="links">
<a href="/resources" class="btn outline-black-transparent">Learn more</a>
</div>
</section>
{% endif %}
现在模板正在按我预期的方式工作。
关于drupal - 将名称变量与 Drupal 8 twig 模板中的字符串值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44373508/