Drupal - why are Twig template variables with markup not being rendered?

Because twig auto-escapes unsafe content and your content isn't marked as safe.

You can explicitly tell it that it is with $test = Markup::create($test); but you should be very careful with that. If it contains any sort of user input, you might open yourself for XSS issues.

Better approaches are using a render array like ['#markup' => 'your text'] which by default will allow a certain amount of html tags but not all of them. You can also use an inline template as documented here: https://www.drupal.org/node/2311123.

Twig natively understands render arrays, so it doesn't matter for your template if you provide a render array or a string.


These are two possibilities to wrap $test in a paragraph and pass it to the twig template:

$variables['test'] = [
  '#markup' => $test,
  '#prefix' => '<p>',
  '#suffix' => '</p>',
];

$variables['test'] = [
  '#type' => 'inline_template',
  '#template' => '<p>{{ text }}</p>',
  '#context' => [
    'text' => $test,
  ],
];

Tags:

Theming

8