0

We have a field, field_text, that we are displaying on the Basic Page content type. It needs to display with double quotes " using twig template. We tried the following below, but it is displaying without the " in the beginning and end.

How can we display the field_text with double quotes (") in twig?

What we tried:

node--page--full.html.twig

<script type="application/ld+json">
  {# None of the following worked #}
  "name" : {{ content.field_text.0 }}
  "name" : {{ '\"' ~ content.field_text.0 ~ '\"' }}
  "name" : {{ '\"' }} {{ content.field_text.0 ~ '\"' }} {{ '\"' }}
</script>
  • Doesn't "{{ }}" work? – 4k4 Apr 1 at 20:04
  • @4k4 when i do "{{ content.field_text.0 }}", it results in "" (an empty value) – usernameabc Apr 1 at 20:35
  • 1
    Then the quotes are working, but the field variable is empty or non-existent. Twig doesn't throw errors for misspelled variable names, it silently returns NULL. – 4k4 Apr 1 at 20:46
  • @4k4 it was indeed misspelled variable name. – usernameabc Apr 2 at 0:22
0

Try with the value of the field and then concatenated with " like the second way: I tested it with the following:

"name" : {{ content.field_text_test.0['#context']['value'] }} <br>
"name" : {{ '\"' ~ content.field_text_test.0['#context']['value'] ~ '\"' }} <br>
"name" : {{ '\"' }} {{ content.field_text_test.0['#context']['value'] ~ '\"' }} {{ '\"' }}

Here is the result: enter image description here

  • it was the fact that i misspelled the name. i was missing a letter and it caused the issue. – usernameabc Apr 2 at 0:22

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.