Score:1

Passing preprocess function variables to a template

er flag

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.

mytheme.theme

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

page--front.html.twig

{% 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?

Score:3
gb flag

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 } %} 
Score:3
in flag

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.

milawithict avatar
er flag
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] (https://drupal.stackexchange.com/questions/288247/how-can-i-pass-an-array-from-a-controller-to-a-template) 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.
milawithict avatar
er flag
The following code is from sandwich.module ``` function example_theme($existing, $type, $theme, $path) { return array( 'example__p1' => array( 'variables' => array('items' => array(), 'title' => ''), ), ); } ```
milawithict avatar
er flag
[link] (https://www.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.
I sit in a Tesla and translated this thread with Ai:

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.