Pass variables from one preprocess function to another in .theme I am developing a theme in Drupal 8 and I have 2 twig files. One is page.html.twig and the other is status-messages.html.twig.

The problem is that status-messages has a variable named message_list (an array) but this variable is protected, so other files don't have access to it. I have tried to call this variable from theme_preprocess_page() like so  $variables['testvar'] = $variables['message_list']; with result of $variables['testvar'] = null.

In the next step I tried to copy the functionality of status-messages.html.twig from Drupal\Core\Render\Element\StatusMessages to mythemem.theme file, like so:

use Drupal\Core\Render\Element\StatusMessages;

function MYTHEME_preprocess_page(&amp;$variables) {

  // Create Dynamic path for project root elements (in general files that are in our theme project)
  $variables['theme_path'] = base_path() . $variables['directory'];

  // this here gives us the array of renderMessages() but  '#message_list' has no data in it
  $variables['testvar'] = StatusMessages::renderMessages(); 
}


function mythemem_preprocess_status_messages(&amp;$variables) {

  // this here dose gives the value but it can be accessed  only in status-messages.html.twig
  $variables['mymessages'][] = $variables['message_list'];

  // same as in mythemem_preprocess.page
  $variables['testvar'] = StatusMessages::renderMessages(); 
}


My aim here is to understand how to correctly use .theme files, how to pass protected variables from one preprocess function to another.



To be more clear I would like to know/understand how to get the same effect for my custom.html.twig file as it is here

To achieve this, I want to take $variables['message_list'] to process it as I want.
well after finking a lot about this problem, i realized that i don't wont to duplicate this variable at all.

lets start from beginning i wanted to copy the $variables['message_list'] from status-messages.html.twig to make my custom layout for different situation. 

for example i wanted to have different layout/design of status message on  my custom login page and another layout/design for my custom register page. but now i understend that i can have this two in one place (in status-messages.html.twig) i can check if my URL is /login tean  with help of twig if condition i can render one layout and if  my URL is /register then i could have second layout! and in aver situation u would have default layout.

is that correct? how would you solve the problem if you ware in my place