Here is a slice of code from my twig template, which outputs the contents of some fields into a p tag:
<p>
{{ content.field_story_program_name }},
{{ content.field_story_relationship}},
{{ content.field_story_city }},
{{ content.field_story_state }}
</p>
The output on the page renders a trailing white space after each field's content, which I don't want (because it creates spaces before the commas):
Local Head Start Program Provider , Head Start administrator , Los Angeles , CA
Things I've tried:
- Based on this post, I checked my field.html.twig template to see if it's introducing spaces (it's not).
- Tried putting a - in the
{% endfor %}
on that field.html.twig template anyway to see if it would help:{% endfor -%}
(it didn't). - Put all of my values on one line in case a carriage return was being interpreted as whitespace (I think that was a Drupal 7 issue that I read) as in:
{{ content.field_story_first_name }} {{ content.field_story_last_name }}, {{ content.field_story_program_name }}, ...etc
But that didn't fix it - Tried
{% spaceless %}
though I know it's just for spaces between tags and so of course that didn't end up helping - Using the
|trim
Twig filter, which makes the entire value disappear and produces a rendering error. I suppose that technically solves the whitespace problem... ;)
Can anyone help give me a clue? I would like to either
- Find out where the extra space is coming from and turn it off there, or
- Use the appropriate Twig filter to remove the space
|trim(side='right')
or perhaps|raw|trim(side='right')
if your field contains markup? Are your field perhaps containing linebreaks? Try using|replace({ "\n": "" })|trim(side='right')