0

I've found twig templates an attractive way to display long text descriptions in Drupal 8, since they can be translated using the {%trans%} and {%endtrans%} tags, and they are much easier to maintain than long lines of text declared in t() functions, where you need to quote special characters.

The drawback of this is that it's not very flexible. The way I'm doing it now works as follows.

  • I implement a mymodule module, and create a templates/ directory under the module directory.
  • I create my my_description.html.twig file, and put it in the templates/ directory.
  • I expose the twig template by using hook_theme():


  function mymodule_theme() {
    return [
      'my_description' => [
         'variables' => [
           'admin_link' => NULL,
         ],
       ],
    ];
  }
* I create the page controller as usual, with a router to a member function, say, description():


public function description() {
  $link = ...;

  return [
    '#theme' => 'my_description',
    '#admin_link' => $link,
  ];
}

This is more code than I'd like the write, if I could get away with it. And I can't figure out how to override the the original my_description.html.twig file. What I'd really like to do is something more like this:


public function description() {
  $variables = [
    'admin_link' => ...,
  ];
  $twig_service = \Drupal::service('some_service_name');
  return $twig_service->someRenderFunc($path_to_template, $variables);
}

The advantage here is it would be much easier to write a general module for this specific purpose. The theme system isn't really the right thing to use here, especially since I'd rather implement a simple class that other modules can override. Template overrides AFAIK is not something a module can do, and I don't want to do this with a theme, where you can do template overrides. Using hook_theme() does not let me flexibly specify where the templates are, since the theme system wants twig files to live in one and only one place.

Is this a complete abuse of Twig in Drupal 8? I actually did this successfully in Drupal 7 (!), by integrating Twig directly using composer_manager. It would be nice to do this simply in Drupal 8 as well.

  • 1
    Templates can still be overridden from a module, hook_theme_registry_alter hasn't been removed; leaving opinion on whether choosing to ignore the in-built template system is a good idea aside, it sounds like what you're looking for is inline templates – Clive Jun 16 '16 at 8:12
  • @Clive, you should turn that into the answer here; I had been unaware of the class, but yeah, that will do fine. It doesn't load from a file, but of course, I can't ask for everything to be done for me here, neh? – Torenware Networks Jun 16 '16 at 20:24
  • Also: I'm not really replacing a template, but making it simple to display a great deal of text that's easily maintained, possibly more than once in an application. Since hook_theme_register_alter still leaves me with but a single template, it has the same problem as using hook_theme in the first place. So using the inline template class is exactly what I want. – Torenware Networks Jun 16 '16 at 20:29
  • @Clive: just gave you props for drupal.org/node/2749563. Thanks much. – Torenware Networks Jun 17 '16 at 4:12

Your Answer

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

Browse other questions tagged or ask your own question.