Skip to main content
1 of 3
No Sssweat
  • 31.8k
  • 14
  • 57
  • 92

How to safely render node body on a custom variable?

I created a custom template for my programmatically created block.

I created a variable for it which is called test

so in my render array I have

'#test' => $node->get('body')->value 

Problem is when I print {{ test }} in my twig template, it literally prints the html rather than rendering it. Ex: <p> My node body </p>

I've also have tried:

'#test' => $node 

Then on twig {{ test.body.value }}

but same result, the html does not get rendered.

The only way I found to make render is by doing {{ test.body.value|raw }} but raw filter should never be used on user entered data.

How can I make Drupal stop escaping the html safely?

No Sssweat
  • 31.8k
  • 14
  • 57
  • 92