custom twig templates in modules

Steps to create custom templates using twig in drupal 8 modules

function custom_twig_theme($existing, $type, $theme, $path) {
  return array(
    'custom_twig_template' => array(
      'variables' => array('test_var' => NULL),
    ),
  );
}

Call the defined template in your controller

/**
 * @file
 * Contains \Drupal\custom_twig\Controller\TestTwigController.
 */
 
namespace Drupal\custom_twig\Controller;
 
use Drupal\Core\Controller\ControllerBase;
 
class TestTwigController extends ControllerBase {
  public function content() {
 
    return array(
      '#theme' => 'custom_twig_template',
      '#test_var' => $this->t('Test Value'),
    );
 
  }
}

Define the template in templates folder of your custom twig module with  template name dashed instead of underscores.  

 

custom-twig-template.html.twig