I'm looking to add a twig variable, which is set in a module, and render it in the html.html.twig template file for my theme.

In my module file I use the hook_preprocess_html() function where I render and set the twig variable like so:

function insusite_cta_bar_preprocess_html(&$variables) {
 $output = \Drupal::theme()->render('insusite_cta_bar_actions', array('items' => $itemsOutput, 'panels' => $panelOutput));

 $variables['cta_bar'] = $output;

Then in the html.html.twig template file in my themes folder I simply render the new cta_bar variable like so:

<!DOCTYPE html>
<html{{ html_attributes }}>
  <head-placeholder token="{{ placeholder_token|raw }}">
 <title>{{ head_title|safe_join(' | ') }}</title>
  <css-placeholder token="{{ placeholder_token|raw }}">
  <js-placeholder token="{{ placeholder_token|raw }}">
 <body{{ attributes }}>
  {{ page_top }}
  {{ page }}
  {{ page_bottom }}
  {{ cta_bar }}
  <js-bottom-placeholder token="{{ placeholder_token|raw }}">

This doesn't render anything at all.

However, if I add preprocess_html() function to my .theme file then it DOES work, problem is we're using drupal as a multi-site system so I'd have to find a way to add this in to every theme's html, is there a way to do this?

1 Answer 1


Just tested what I can of your code, and this works from a custom module:

function examplemodule_preprocess_html(&$variables) {

 $variables['cta_bar'] = ['#markup' => '<h2>Test CTA Bar</h2>'];

It then can be output in the theme with this template:


like this:

{{ cta_bar }}

Of course, you need to register your preprocess function by clearing the cache.

Other than that, I think it must be a problem with your theme function insusite_cta_bar_actions. Can you give us more info on that?

  • Right, tried again today, everything seems to work right now like I intended . Don't know what changed, but like you said probably someting else in my fuction was causing this to break. Jun 14, 2016 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.