Render multiple value fields in node template

Created on 31 July 2016, over 8 years ago
Updated 16 July 2023, over 1 year ago

You can access single value field in node template easily:

{{ content.field_test }}
or
{{ node.field_test.value }}

But iteration over multiple value fields is not pretty:

{% for key, item in content.field_tags if key|first != '#' %}
  <div class="item-{{ key + 1 }}">{{ item }}</div>
{% endfor %}

You can, of course, use a field template, but sometimes it doesn't make sense to have a ton of field template when you can simply have one node template.

Example:

I need to add "," between a field's values (a multiple value entity references field rendered in label). So I have a field template with:

{% for item in items %}
  {{ item.content }}{% if not loop.last %},{% endif %}
{% endfor %}

And in my node template I have:

{% if content.field_facilities is not empty %}
   <p>{{ content.field_facilities }}</p>
{% endif %}

But I would prefer to have a similar code directly in my node template like:

{% if content.field_facilities is not empty %}
   <p>
     {# This do not works! #}
     {% for item in content.field_facilities %}
       {{ item.content }}{% if not loop.last %},{% endif %}
     {% endfor %}
   </p>
{% endif %}

What would be the best way to solve that?

See also:

Feature request
Status

Needs work

Version

11.0 🔥

Component
Theme 

Last updated about 3 hours ago

Created by

🇨🇭Switzerland gagarine

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇮🇳India ranjith_kumar_k_u Kerala

    Re-rolled #31

  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    Happy to see there are tests in there.

    The issue summary should be updated with what is being added, The "why" is there. Would recommend using default issue template.

    If we are adding a new twig function or way to render it will need a change record.

    =====

    Just FYI to help get the message out there.

    Starting March 2023, simple rerolls, rebases, or merges will no longer receive issue credit. Only rerolls that address a merge conflict will be credited, and the merge conflict that was resolved must be documented in the text of an issue comment.

    Example
    error: patch failed: core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module:77
    error: core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module: patch does not apply

    To receive credit for contributing to this issue, assist with other outstanding tasks or unaddressed feedback.
    See the issue credit guidelines for more information.

  • 🇦🇺Australia sime Melbourne

    Wow, this gets the award for the simplest patch I didn't know i needed. Very neat.

Production build 0.71.5 2024