0

How can I access node property which name starts with # in twig template?

I have content type with field Category and I need to access value of that field inside of twig template?

Using dump(content.field_category[0]) I get this

array (size=5)
  '#type' => string 'link' (length=4)
  '#title' => string 'Example' (length=5)

How to access #title ?

Thank you.

1

You can add this line, {{ content.field_category[0]['#title'] }} to get value of title inside twig.

  • 2
    You don't need the filter | raw to get a plain text like a title and it's also a bad example, because if the variable would contain unsafe data you don't want to have it unescaped in the template. – 4k4 Jan 9 '17 at 19:25
  • It is for getting raw value as i said in answer. No need to provide raw if it only plain text – Ashish Deynap Jan 9 '17 at 19:34
  • 1
    Sorry for nitpicking, but raw is unsafe and should not be applied to variables with user input, see drupal.org/docs/8/theming/twig/… – 4k4 Jan 9 '17 at 19:42
  • Thanks for the information as i did not knew that. – Ashish Deynap Jan 9 '17 at 19:46

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.