I'm trying to create a test twig template in drupal 8, I followed "Create custom twig templates for custom module" from their website but it's not working for me for some reason. This is what I have:
Contents of folder test_twig_template:
test_twig_template.info.yml:
name: Test Twig Theme
type: module
description: Module to test if twig theme is working as intended
package: Custom
version: 1.0.0
core: 8.x
test_twig_template.module:
<?php
function test_twig_template_theme($existing, $type, $theme, $path) {
return [
'my_template' => [
'variables' => ['test_var' => NULL],
],
];
}
test_twig_template.routing.yml:
test_twig_template.theme:
path: /twig
defaults:
_controller: Drupal\test_twig_template\Controller\TestTwigTemplateController::content
_title: 'Twig title'
requirements:
_permission: 'access content'
Code inside test_twig_template/src/Controller/TestTwigTemplateController.php:
<?php
namespace Drupal\test_twig_template\Controller;
use Drupal\Core\Controller\ControllerBase;
class TestTwigTemplateController extends ControllerBase {
public function content() {
return array(
'#title' => 'Test Twig title',
'#theme' => 'my_template',
'#test_var' => $this->t('Test Value'),
);
}
}
And finally inside test_twig_template/templates/my-template.html.twig I have:
<p>Test twig template!</p>
<p>test_var: {{ test_var }}</p>
Thing is if I remove this "'#title' => 'Test Twig title'," from my controller nothing gets displayed on my custom page. Otherwise it just displays this title "Test Twig title". Am I missing a call for twig somewhere or are my hooks_theme() not properly set up?
test_twig_template
,test_twig
andtest_twig_theme
for the module’s machine name. You need to pick one and stick with it