<?php
Step #1: Define hook_theme in .module file
function MODULE_NAME_theme($existing, $type, $theme, $path) {
return [
'my_template' => [
'variables' => ['var_test' => NULL],
],
];
}
Step #2: Call the Template
/**
* @file
* Contains \Drupal\MODULE_NAME\Controller\TestTwigController.
*/
namespace Drupal\MODULE_NAME\Controller;
use Drupal\Core\Controller\ControllerBase;
class ModuleNameController extends ControllerBase {
public function content() {
$renderable = [
'#theme' => 'my_template',
'#var_test' => 'variable test',
];
$rendered = \Drupal::service('renderer')->render($renderable);
}
}
Step #3: Create Twig Template
//the file name would be my-template.html.twig
<p>Test twig template!</p>
<p>test_var: {{ var_test }}</p>
Remember to Clear the cache.
?>
- Add new comment
- 1840 views