Score:1

What does #markup mean?

us flag

I meet this array key all the time in Drupal (for example in Symphony's dpm()) yet I am not able to find any documentation about it, what it does, how can I use it? How do I display something that is given as #markup value in Twig?

ru flag
Take a look a the documentation for render arrays: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/group/theme_render/9.3.x
Score:4
us flag

By using #markup in render arrays you can output raw HTML (passed through XSS filter to ensure security).

Here's an advanced example, with optional #allowed_tags and placeholders.

/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_foobar(&$variables) {
  $variables['video'] = [
    '#markup' => '<span>@video_label:</span> <video><source src="v.webm" type="video/webm"></video>',
    '#allowed_tags' => [
      'video',
      'source',
    ],
    '#attached' => [
      'placeholders' => [
        '@video_label' => t('Sample label'),
      ],
    ],
  ];
}

In Twig you just do:

{{ video }}

And in your browser you get the HTML with replaced placeholder and stripped <span>:

Translated label: <video><source src="v.webm" type="video/webm"></video>
4uk4 avatar
cn flag
I would use a longer name instead of `@label`, this is a global placeholder replacing everything on the page it can find. Or use FormattableMarkup: `'#markup' => new FormattableMarkup('<span>@label</span>', ['@label' => $label])`
us flag
You are right here, I just wanted to show how attached placeholders work here. I've changed the name of the label to more specific one.
Score:1
in flag

Just by using the regular print twig syntax: {{ variable }}

The #markup is the html that will be on the final page sent from the server.

You can use a preprocess function to intercept and change the #markup if you need to.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.