In the Drupal 8 documentation I have found a way how to pass variable from controller to custom twig template



function test_twig_theme($existing, $type, $theme, $path) {
  return [
    'my_template' => [
      'variables' => ['test_var' => NULL],


class TestTwigController extends ControllerBase {
  public function content() {

    return [
      '#theme' => 'my_template',
      '#test_var' => $this->t('Test Value'),


But is there a way to pass variables from controller to already existing node template, for example to node--custom_content_type.html.twig

2 Answers 2


I believe that node--custom_content_type.html.twig is referring to template overriding. In template overriding, you are still using the default theme with it's defined variables. If you are generating the render block in your controller, you can set the variables defined in the default theme. These are generally listed in the default template.

An alternative to passing variables into templates is to use the specific theme preprocess hook.

Finally, if you set up your local site to deal with debugging twig, you will see information about the template and available hooks for various parts of the output. See the docs for how to set this up.


Your custom controller is not a node so it doesn't make sense why you would pass custom values to a node template. If you are wanting to override a node itself then you would look into hook_preprocess_node and providing that value in the template through that means.

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.