drupal - 将名称变量与 Drupal 8 twig 模板中的字符串值进行比较

标签 drupal twig drupal-8 drupal-taxonomy

我正在 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/

相关文章:

Drupal 7 - 将图像添加到 node.tlp.php 主题文件

symfony - 商店 6 : Twig files not loaded when using sw_include

jquery - 为什么我的 Drupal 8 CORS 设置不起作用?

twig - Yii 2 在 Twig 模板中注册Js

drupal - 如何通过 Drupal 8 中的模块修改节点的主体

Drupal 8 - 节点引用迁移到实体引用类型

javascript - Drupal - 如果关闭 JS,是否有特殊的 css 文件?

linux - 如何使用 Linux root 用户访问服务器登录 drupal admin?

javascript - 编辑drupal节点时使用javascript/jquery修改edit-body字段

php - 使用 symfony2 和 twig 创建文件文本