I try to print field value in a node--template.html.twig without whitespace. This field is a select in admin page.


This is how my value is print :

{{ content.field_type }}

enter image description here

field.html.twig :

{% for item in items %}
{{- item.content -}}
{%  endfor %}

I try some twig argument like


But when i print like this, it's empty.

So where are this whitespace ? same if i try to test the value in if else condition :

This doesn't work because of whitespace :

{% if content.field_type == "Achat" %}

This work :

{% if content.field_type|render|trim == "Achat" %}

So for test a value it's ok, but for print the value if i use


i got same result with whitespace.

So if someone can tell me where is add this whitespace and how remove this in twig. I use the boostrap theme, but i don't see anything who add whitespace.

  • Have you tried using spaceless? twig.sensiolabs.org/doc/2.x/tags/spaceless.html
    – Kevin
    Commented Mar 14, 2017 at 14:16
  • yes, same result
    – Kevin
    Commented Mar 14, 2017 at 14:18
  • 1
    Also you don't want to do checks on the render array, do it on the field itself via node.field_type.value == 'Achat'
    – Kevin
    Commented Mar 14, 2017 at 14:24
  • Agree for the test, but for the print ? why whitespace ?
    – Kevin
    Commented Mar 14, 2017 at 14:25
  • @Kevin about your comment please : could you develop a bit why /when checking on entity object over content render array ? e.g profile.some_field vs content.some_field ? my template say in comments :Use 'content' to print them all, or print a subset such as 'content.title'. Tks :)
    – Kojo
    Commented Nov 20, 2019 at 15:13

2 Answers 2


It sounds to me that you have a template, probably field.html.twig that is adding this space. You can enable the twig debug to see which templates' files are used.

Also, I think it's better to test the value on node.field.value instead of content.field as it's done for the rendering only.

Note that the twig operators as {% for ... %} also add spaces, so you should add - as well in there for e.g.:

{%- for item in items %}
{{ item.content }}
{%  endfor -%}
  • The template use is field.html.twig, i just add - in {%- for, but it's the same, whitespace always here... i agree for test the value to use node.field.value.
    – Kevin
    Commented Mar 14, 2017 at 15:45
  • What is this hyphen doing? And why isn't this default behavior?
    – leymannx
    Commented Apr 19, 2018 at 13:53
  • 1
    Ah! Whitespace Control Modifier. Gosh is this ugly. This really should be a global default.
    – leymannx
    Commented Apr 19, 2018 at 14:03

I tried many times and spaceless tag works pretty well.

Use the following syntax:

{% spaceless %}


{% endspaceless %}
  • 3
    i still got whitespace
    – Kevin
    Commented Sep 6, 2017 at 8:47
  • I, too, was thoroughly disappointed.
    – Randell
    Commented Jul 6, 2018 at 1:53
  • Interesting enough, this worked for me for a separate project.
    – Randell
    Commented Oct 4, 2018 at 22:24
  • maybe you didn't clear the drupal cache?
    – Robbiegod
    Commented Aug 13, 2019 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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