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 "
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:
- Why is this space being introduced?
- 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...