2

I am trying to iterate over a Paragraph field and apply a comma after each Paragraph item.

In the field template that outputs the Paragraph, I am trying:

{%- for item in items %}
  {{ item.content }}{{ not loop.last ? ', ' }}
{% endfor -%}

This puts the comma out, but there is a ton of whitespace all over:

enter image description here

I have tried many whitespace control combinations and none seem to have any effect.

How can I remove this sort of behavior entirely? The paragraph field is an entity reference, that renders a view mode, so I don't want to install field delimeter plugin.

edit: Tried the solutions mentioned here:

How remove whitespace on field in twig?

and here:

https://craftcms.stackexchange.com/questions/2789/whitespace-created-when-using-if-statements

as well as in the documentation.

The structure is as follows:

Node -> Paragraph field -> Paragraph (Entity) -> User Reference + Plain text field + rich text

In this context, only the user field is being rendered, and I want to put a comma at the end of each linked name.

  • You should provide the white space control combos u alrdy tried. – No Sssweat May 2 at 21:15
  • Is this paragraph field inside a view? – No Sssweat May 2 at 21:26
  • Nope, field on a node. – Kevin May 2 at 21:28
  • 1
    If you switch to the Bartik theme, does the same issue persist? – No Sssweat May 4 at 12:23
0

Although the whitespace control modifier is supposed to work for all {{ … }} prints, {% … %} statements and {# … #} comments I'd expect to make your sample work it needs to be put directly into the print like that:

{% for item in items %}
  {{- item.content -}}{{- not loop.last ? ', ' -}}
{% endfor %}

Alternatively you could try to use the {% spaceless %} tag. But again, I'd expect this only works right when placed inside the loop:

{% for item in items %}
  {% spaceless %}
    {{ item.content }}{{ not loop.last ? ', ' }}
  {% endspaceless %}
{% endfor %}

As of Twig 1.38, use the |spaceless filter instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.