Theming custom block in twig file and render variables I created a custom block and I'm trying to render it in twig file. The file gets used, as I can see its output, but my variable 'my_variable' defined in the block is not rendered:

in CustomBlock.php

public function build() {
    $build = [];

    $build['custom_block'] = [
      '#my_variable' =&gt; 'abcdef',
      '#theme' =&gt; 'custom_block',
    ];

    return $build;
}


in my_module.module:

function my_module_theme() {
  return [
    'custom_block' =&gt; [
      'template' =&gt; 'custom-block',
    ],

  ];
}


in custom-block.twig.html:

&lt;div&gt;{{ my_variable }}&lt;/div&gt;


-> The twig file gets used and the div is shown, but the variable is empty/is not rendered.
You probably just need to define the available variables:

function my_module_theme() {
  return [
    'custom_block' =&gt; [
      'template' =&gt; 'custom-block',
      'variables' =&gt; ['my_variable' =&gt; ''],
    ],

  ];
}


You'll need to rebuild caches for the change to be picked up.