In a custom module I specified in the hook_preprocess_page function the following session variable:

if (isset($blockNameState) && $blockNameState == 'open') {
   $tempstore->set($blockNameState, 'close');
else {
   $tempstore->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->get($blockNameState);

1 Answer 1


You can inject new variables into templates by using the appropriate preprocess hook in your theme:

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

Then in the corresponding template:

{{ myvariable }}

You might have caching issues with this though.

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.