Passing variables to a block template I am using the following code, but it doesn't work.

drupal_block.module

function custom_blocks_theme($existing, $type, $theme, $path) {
  return [
    'custom_blocks__front_apps' =&gt; [
      'variables' =&gt; [
        'app' =&gt; NULL
      ],
    ],
  ];
}


BlockTest.php

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#theme' =&gt; 'custom_blocks__front_apps',
      '#app' =&gt; 'test value',
    ];
  }


custom_blocks__front_apps.html.twig

&lt;p&gt;Hello: {{ app }}&lt;/p&gt;


My block is empty.


  


What is wrong with the code?
I think you should write it like below code.
   /**
    * Implements hook_theme() to add the template definition. 
    **/
    function lotus_theme($existing, $type, $theme, $path) {
      return [
        'lotus_template' =&gt; [
          'variables' =&gt; ['test_var' =&gt; NULL],
        ],
      ];
    }

In your controller file
public function build() {

    return array[
      '#theme' =&gt; 'lotus_template',
      '#test_var' =&gt; $this-&gt;t('Test Value'),
    ];

  }

Then in your lotus-template.html.twig
&lt;p&gt; This is the lotus template with a value of {{ test_var }} &lt;/p&gt;

Source Define a custom template for module output