I'm trying to do something you'd think was simple, but I don't find good documentation for it.
I'm trying to create a controller that displays a twig template.
I've done the following so far:
1) In my module directory, I've created a templates/ directory. It contains a field-permission-description.html.twig
file, containing mostly HTML text. So far, I don't even have any {{ twig_variables }}
in it:
<div class='examples-description>
<p>Field permision text will be here.</p>
</div>
2) I have a theme function in my MY_MODULE.module file:
/**
* Implementation of hook_theme
*
* Since we have a lot to explain, we're going to use Twig to do it.
*/
function MY_MODULE_theme($existing, $type, $theme, $path) {
return [
'field_permission_description' => [
'variables' => [
'stuff' => NULL
]
]
];
}
3) In my controller, the function returning the render array looks like this:
/**
* A simple controller method to explain what this example is about.
*/
public function description() {
$build = [
'description' => [
'#theme' => 'field_permission_description',
'#stuff' => 'some stuff',
]
];
return $build;
}
I clear the cache with drush, and go to the page. There's no error, even in the watchdog, but the content region of the description page is blank; the template does not render.
It's not a problem with the controller, since if I substitute something else (say, just a little HTML in a #markup
key), it renders fine.
There's been a couple of answers a year or two ago, but these all have things that no longer work as of RC1.
What do I need to do to get a twig template to render the way I'm trying to do here?