Situation: I want to write a proprocess PHP function [theme]_preprocess_example__p1 that adds variables to a page template file example--p1.html.twig but the variable cannot display properly if I inlcude the template to page--front.html.twig.
page--front.html.twig
I added the following code in page--front.html.twig
{% include '@[theme]/directory/example--p1.html.twig' %}
example--p1.html.twig
this file has one variable {{ text }}
. to display the text message.
[theme].theme
I added the following code in [theme].theme
[theme]_preprocess_example__p1(array &$variables, $hook){
$variables['name'] = 'message';
$variables['text'] = 'example p1 message';
$variables['data'] = 'm1';
}
but I cannot display text variable to the template example--p1.html.twig when I added include {% include '@[theme]/directory/example--p1.html.twig' %} in page--front.html.twig.
Someone suggest creating all the variables to ExampleController.php and use example.module to define hook_theme() function and function [theme]_preprocess_example__p1() but it is failed to display the variables when I include the template example--p1.html.twig for duplicated code to page--front.html.twig template. How can I solve it?