How to render a template in a module controller? I want to render a custom template in a module controller but I don't know how. How do I achieve this?
You need to let Drupal know about the theme, and then you can leverage it in the controller code. The template needs to be in the folder my_module/templates.

my_module.module

function my_module_theme() {
  $theme['template_name'] = [
    'render element' =&gt; 'build',
    'template' =&gt; 'template_file_name',//This is only needed if you need a different template file name than the array key above, 'template_name'.
  ];

  //override existing template for this module
  $theme['html__my_module__template_file_name'] = [
    'template' =&gt; 'html--my_module--template_file_name',
  ];
  return $theme;
}


src/Controller/MyModuleController.php

public function index() {
  $build['#theme'] = 'template_name';
  //populate your build renderable array..
  return $build;
}