I am trying to include another template file with pre-process's theme hook variables in a template file but it failed. I checked theme debug and it does not include example's pre-process theme hook. Can you give me guidance?

I added {% include 'example--p1.html.twig' %} in the page--front.html.twig file. The example--p1.html.twig file has one variable, {{ test }}. Also, I created a pre-process function that adds variables to a page template. The code is the following.

function mytheme_preprocess_example__p1(array &$variables, $hook) { 
  $variables['test'] = 'please help';
  $variables['test_var'] = 'this is my second variable';

It doesn't appear in the example--p1.html.twig template.

Someone suggested using the following code in the page--front.html.twig file.

{% include 'example--p1.html.twig' with {'test' : 'this is my message'} %} 

I then added the following code in the following files.


function mytheme_preprocess_example__p1(array &$variables, $hook) { 
  $variables['content']['text'] = 'second';


{% include 'example--p1.html.twig' with {'test' : 'content.text'} %} 

It works as it shows the text message value in example--p1.html.twig and page--front.html.twig.

How can I pass pre-process function's text messages' variables to page example--p1.html.twig and then I can display all the variables if I include example--p1.html.twig to page--front.html.twig?

2 Answers 2


Preprocess functions are part of a render setup that includes at least:

  • hook_theme() to register the template to Drupal.
  • hook_preprocess_HOOK to preprocess template variables before it's handed to Twig.
  • The twig template to represent the HTML of the template.
  • Something that "builds" the interface using the above setup.
    • This involves creating a render array with #type set to the registered template's name to render the data using the template. Examples of this are field formatters, controllers, blocks, etc.

The general flow of a render is build -> preprocess -> twig = HTML

Essentially a long-winded way of saying not all twig templates have preprocess functions. Twig templates that are just "included" are not part of this mechanism and won't have preprocess function.

  • Thank you for your suggestion. I tried to build custom module to custom template and defined the variables in controller. However, it cannot get any variables from controller to template. I have read [link] (drupal.stackexchange.com/questions/288247/…) the article for passing variables to template namely example--p1.html.twig but it still doesn't work for me. I can get the controller page and get html contents but it cannot render the data from controller. Commented Jan 10, 2023 at 9:01
  • The following code is from sandwich.module ``` function example_theme($existing, $type, $theme, $path) { return array( 'example__p1' => array( 'variables' => array('items' => array(), 'title' => ''), ), ); } ``` Commented Jan 10, 2023 at 9:01
  • [link] (youtube.com/watch?v=mHTuXXfBs7Y) The example code just likes the above video "Developing a Drupal 8 module with a controller and custom template" but I cannot get the variables from controller and display them in my custom template. Commented Jan 10, 2023 at 9:03

For your front page twig: page--front.html.twig -- you would want to add a variable in the THEMENAME_preprocess_page function.

Preprocess functions would essentially match what the twig is targeting -- page data. If your twig was for a node, then you'd want a preprocess_node.

function THEMENAME_preprocess_page(array &$variables, $hook) { 
  if (\Drupal::service('path.matcher')->isFrontPage()) {
    $variables['some_variable'] = 'some value';
  } else {
    $variables['some_variable'] = 'a different value';

And then I think your include just needs to do this

{% include 'example--p1.html.twig' with {'test' : some_variable } %} 

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.