0

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?

  • 1
    You’re simultaneously using test_twig_template, test_twig and test_twig_theme for the module’s machine name. You need to pick one and stick with it – Clive Nov 8 at 10:06
  • I wasn't using 'test_twig' anywhere in my code, but I was mixing other two. Changed that, now it is 'test_twig_template' everywhere from module name to everything else. Still it's not working. Do you have any idea what else might be wrong?@Clive – user97041 Nov 8 at 10:34
1

It looks like you are messing around with your naming.

Since your module is called test_twig_template you must use that name for hooks and namespaces etc.

Fix these things and it should work.

  • Rename your hook_theme() from test_twig_theme to test_twig_template_theme.
  • Call your route test_twig_template.theme.
  • Your controller definition in your route must look like this, _controller: \Drupal\test_twig_template\Controller\TestTwigThemeController::content.
  • The namespace in your controller must be namespace Drupal\test_twig_template\Controller;.
  • Rename test_twig_template/src/Controller/TestTwigController.php to test_twig_template/src/Controller/TestTwigThemeController.php (the file name must match the class name).

That should be all, since your module is called test_twig_template it's important to use that name in all namespaces for your code to work properly :)

And then as @Hudri mentions your template shall only contain:

<p>Test twig template!</p>
<p>test_var: {{ test_var }}</p>

Head and body tags are provided by a general template in your theme.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Nov 8 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy