问题
i'm creating a module my_module, I want to tempstore a variable and get it inside a twig file. here my controller:
$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule');
$tempstore->set('response', $response);
twig:
{{ ?? like response }}
回答1:
First register your theme in module file
function hook_theme($existing, $type, $theme, $path) {
return [
'my_template' => [
'variables' => ['test_var' => NULL],
],
];
}
Second call this theme from controller and pass the variable
$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule');
$tempstore->set('response', $response);
return [
'#theme' => 'my_template',
'#test_var' => $tempstore,
];
Third render test_var in twig
<p>test_var: {{ test_var }}</p>
来源:https://stackoverflow.com/questions/57040567/get-tempstore-inside-a-twig-file-drupal-8