All the examples for creating custom block online return #markup from build(), but I'd like to return a render array and have it use a template that's part of the module.

Would love an example of how to do this.

  • It's not different from what done with Drupal 7, with #theme. Keep in mind that in Drupal you never access a template file directly.
    – apaderno
    Nov 19, 2015 at 3:25
  • See this useful tutorial. Apr 6, 2016 at 8:38

1 Answer 1


An array with #markup is a render array, just the most basic version of it. If you want to use a theme or template, then just do so. Using and defining templates hasn't changed that much compared to D7, the biggest difference is that they're now html.twig files and not .tpl.php.

  • Thanks for the replies kiamlaluno and Berdir. I've been trying to add a #theme element with a value of 'block--bfsa' ( the name of the module ) with a template at modules/custom/bfsa/templates/block--bfsa.html.twig and it isn't being picked up. The same template places in the active theme is picked up. Do I have the naming convention, location or something else wrong?
    – Andy
    Nov 19, 2015 at 17:41
  • -- is a special pattern for providing optioanl alternatives of a base templates. That is only supported for themes by default. If you want to use your own template, then pick a name like block_bfsa and implement hook_theme() to register that template. Look at existing hook_theme() implementations for examples, like system_theme(). Looking at that, you can see that there is actually a way to explicitly register suggestions, but unless your template actually is a variant of block.html.twig, I'd not use that name.
    – Berdir
    Nov 19, 2015 at 20:57

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.