0

I am trying to output the label of a node in twig with Quotations around it like this: <h2>"{{ label }}"</h2>

The output for some unknown reason though is: " My Node Title " with spaces. What I need is: "My Node Title", without spaces.

I have tried both {{- label -}} and wrapping it with {{ spaceless }} to no avail. I have also tried using &quot; but that did not change the output either.

The HTML Structure is showing that the extra spaces are not coming from the label field but is somehow being introduced by twig in some way.

My questions are:

  1. Why is this space being introduced?
  2. How can I make it stop?

Thank you in advance!

Edit:

So in this case, the label is a render array and the whitespace is not in the label value anyways. The whitespace is being added presumably by twig between the " and the {{.

Doing {{ label|trim }} produces an error that "trim" expects a string although it is an array.

The whitespace is being added somewhere and using trim would simply be undoing something I didn't intend to do. Ideally, I would like to stop the whitespace from happening initially rather than find a way to remove it later.

Temporary Workaround Edit:

I discovered that, if in my preprocess function I create a new variable like this:

$node->get('title')->getString();    
$variables['new_label'] = '"'.$title.'" '

and then render the new_label in twig with: {{ new_label }}, then I do not get the spaces.

The question remains though how to do this in twig...

6
  • What sets this label value?
    – mona lisa
    Commented Jun 23, 2022 at 0:35
  • This is in a node template file and is using the common label variable. The same thing happens though with something like {{ content.field_my_field }} Commented Jun 23, 2022 at 1:52
  • 1
    Perhaps stackoverflow.com/questions/54794321/…
    – mona lisa
    Commented Jun 23, 2022 at 2:45
  • Have you tried the spaceless tag or other twig white space controls?
    – Kevin
    Commented Jun 23, 2022 at 10:52
  • I have tried both {{- label -}} and wrapping it with {{ spaceless }} to no avail. Commented Jun 23, 2022 at 13:38

2 Answers 2

2

These are probably not single space characters but other whitespace or HTML comment tags coming from the title field template. Try first to switch off any debug output, for example Twig debug. If this doesn't help then render the field value directly without the field template:

<h2>"{{ node.label }}"</h2>

See also How do I get the plain text value for the {{ label }} field in a twig node template?

0

The trim filter strips whitespace (or other characters) from the beginning and end of a string:

{{ '  I like Twig.  '|trim }}
3
  • Thank you for the response but the whitespace is not in the label field but rather being added somehow between the " and the {{ Commented Jun 23, 2022 at 2:00
  • Can you try? <h2>{{'\"' ~ label|trim ~ '\"'}}</h2>
    – Stephen.R
    Commented Jun 23, 2022 at 4:43
  • I tried this but the |trim function won't work on the label array. Commented Jun 23, 2022 at 13:40

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.