Score:0

Using dynamic templates in custom module

ro flag

I have created a custom Drupal 9 module, and I would like for this module to use different template files basing on a variable. I have looked at hook_theme() and I think the pattern return value might be what I am looking for, but I can't seem to make it work.

testpage.module

function testpage_theme() {
  return array(
    'testpage' => array(
      'template' => 'testpage',
      'variables' => array('items' => array(), 'title' => NULL),
      'pattern' => 'testpage__'
    )
  );
}  

### Controller file (Testpage.php)

```php
namespace Drupal\testpage\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;

class Testpage extends ControllerBase {

  public function Render($name) {
    $result = "Lorem ipsum";
    $build = array(
      '#theme' => array('testpage__'.$name, 'testpage'),
      '#items' => $result,
      '#title' => 'Testpage',
      '#cache' => [
        'max-age' => 0
      ]
    );

    return($build);
  }
}

I created three template files in a "templates" folder.

  • testpage.html.twig
  • testpage--test.html.twig
  • testpage--test2.html.twig

Basing on $name, it should use the matching template file. However, it is always just using the markup from the testpage.html.twig. When $name is equal to "test", it should use the testpage--test.html.twig file; when $name is equal to "test2", it should use the testpage--test2.html.twig file. And it would be great if it would use the testpage.html.twig file as a fallback if a matching template does not exist. I thought this was possible with the pattern return value, but I can't seem to get it to work correctly. Am I missing something, or is this not possible?

john Smith avatar
gr flag
one thing u could definitely do is to pass `$name` to the template and in the template use sth. like `{{ include('template-' ~ name ~ '.html.twig') }}`
neessen avatar
ro flag
Yeah thought about that solution too, but that would require that the file exists. I could of course check in the controller if the twig file exists, and pass that info to the render array, and use that logic in the twig template.
ru flag
AFAIK `pattern` [doesn't exist anymore](https://www.drupal.org/project/drupal/issues/2063793) and is just a left-over in the docs. Instead use [hook_theme_suggetions_alter](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme_suggestions_alter/8.8.x)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.