How to access session variables in Twig template? In a custom module I specified in the hook_preprocess_page function the following session variable:

if (isset($blockNameState) &amp;&amp; $blockNameState == 'open') {
   $tempstore-&gt;set($blockNameState, 'close');
}
else {
   $tempstore-&gt;set($blockNameState, 'open');
}


Let's assume the above code ran with $blockNameState = 'block1_state' and there is key/value pair 'block1_state/open' in the $tempstore.

How do I access the key/value pair in a Twig template?

Can't run the following PHP command in Twig:

$blockNameState = $tempstore-&gt;get($blockNameState);
You can inject new variables into templates by using the appropriate preprocess hook in your theme:

function mytheme_preprocess_page(&amp;$variables) {
  // code to get value...
  $variables['myvariable'] = $value;
}


Then in the corresponding template:

{{ myvariable }}

You might have caching issues with this though.