8

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

Location|Location 
Achat|Achat

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

{{content.field_type|trim}}

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

{{content.field_type|render|trim}}

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 Mar 14 '17 at 14:16
  • yes, same result – Kevin Mar 14 '17 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 Mar 14 '17 at 14:24
  • Agree for the test, but for the print ? why whitespace ? – Kevin Mar 14 '17 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 Nov 20 at 15:13
4

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 Mar 14 '17 at 15:45
  • What is this hyphen doing? And why isn't this default behavior? – leymannx Apr 19 '18 at 13:53
  • Ah! Whitespace Control Modifier. Gosh is this ugly. This really should be a global default. – leymannx Apr 19 '18 at 14:03
4

I tried many times and spaceless tag works pretty well.

Use the following syntax:

{% spaceless %}

  {{content.field_type}}

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

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.