Module Development

Add a Template File for Drupal 8, 9, and 10

Template files are used by modules when they need to add custom HTML to the content they output. The most common example is wrapping your output in one or more <div> tags to give it additional structure and context. Using custom template files in a Drupal module requires defining a new theme hook, creating the template file, an associating the appropriate data with the template file via a render array.

In this tutorial, we'll:

  • Learn how to add a custom Twig template file to a module.
  • Update the WeatherPage controller to use the new template file.

By the end of this tutorial you should be able to create and use custom template files in a module.

Drupal Module Developer Guide