【问题标题】:How to loop a paragraph list (entity reference revisions list) in twig drupal 8如何在 twig drupal 8 中循环段落列表(实体参考修订列表)
【发布时间】:2020-04-14 17:15:25
【问题描述】:

如何循环段落列表并获取每个字段的值?

我已经建立了一个包含其他段落的段落。 现在我需要遍历它们并获取它们的值以将它们列出到树枝模板中

想象段落结构有这个字段

  1. 标题
  2. 说明
  3. 段落:

    1. 标题
    2. 说明
    3. 开始日期
    4. 结束日期

我的 field.html.twig 文件看起来像

{% if label_hidden %}
  {% if multiple %}
    {% for item in items %}
      {{ item.content }}
    {% endfor %}
  {% else %}
    {% for item in items %}
      {{ item.content }}
    {% endfor %}
  {% endif %}
{% else %}
  <div{{ title_attributes }}>{{ label }}</div>
  {% if multiple %}
    {% endif %}
    {% for item in items %}
      {{ item.content }}
    {% endfor %}
    {% if multiple %}
  {% endif %}
{% endif %}

我的段落--name.html.twig看起来像

<div class="row">
  <div class="col-sm-8 col-md-offset-2">
    <div class="section-title">
      <h2>{{ content.field_skill_title }}</h2>
      <p>{{ content.field_skill_description }}</p>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-sm-8 col-md-offset-2">
    {# HERE SHOULD GO THE LOOP WITH REST 3 PARAGRPAHS #}
  </div>
</div>

我的问题是我应该如何实现循环以将值显示到模板中?

【问题讨论】:

  • 你的问题对我来说有点不清楚。什么不工作?

标签: twig drupal-8 paragraph


【解决方案1】:

<div class="row">
  <div class="col-sm-8 col-md-offset-2">
    <div class="section-title">
      <h2>{{ content.field_skill_title }}</h2>
      <p>{{ content.field_skill_description }}</p>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-sm-8 col-md-offset-2">
    {# HERE SHOULD GO THE LOOP WITH REST 3 PARAGRPAHS #}
    {% for key, item in content.field_name_here if key|first != '#'%}
     {{ item['#paragraph'].field_title[0].value|raw }}
     {{ item['#paragraph'].field_description[0].value|raw }}
     {{ item['#paragraph'].field_start_date[0].value|raw }}
     {{ item['#paragraph'].field_end_date[0].value|raw }}
    {% endfor %}
  </div>
</div>

【讨论】:

  • 分享代码时,请尽量在回答中说明你的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
相关资源
最近更新 更多