1

I have produced a simple Drupal module which provides a wrapper for a JavaScript library. I'm working on a separate module which will produce several pages of examples of the library and wrapper in action. These pages will each contain a two sections, the library, and description text and miscellaneous HTML.

What is the best technique to do this the Drupal 8 way? I can imagine two strategies:

  • Each page will be a separate function within my controller. The function can first add two entries to the output array, the first will contain the data for the library, and the second the raw HTML and text.

  • In the second technique, I would place all the HTML and text in a twig template. Then I would add all the data for the library in the controller and add a reference to this new theme template as a second entry in the output array. I like the idea of this second strategy, but I don't know how to accomplish it. I've look at code for several Drupal 8 projects and read a few tutorials, and I haven't seen an example of this. The hook_theme function always contains a single return statement. How would I add a switch or if statement within hook_theme to determine which twig template to return.

If there is a third option that is even better, let me know.

3

hook_theme() returns a list of templates that will then be cached in the registry. You don't need conditions there, in fact, you can not, since it will be called only once.

Just return multiple templates and then reference them in your render arrays, like you already have to when using a single one.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.