I installed js_example, one of the sample modules in the Examples for Developers module.

In this module I found the following configuration:

 - jsweights.js
 - black.js
 - blue.js
 - brown.js
 - green.js
 - js_example_accordion.js
 - purple.js
 - red.js
  - JsExampleController
 - accordion.html.twig
 - description.html.twig

I do not understand how the template description.html.twig and accordion.html.twig are retrieved.

I miss this piece to understand the example completely.

In which file do I find the configuration I am looking for?

  • Drupal manages those files. You declare the templates in the yml file and then Drupal uses them when needed.
    – sanzante
    Jun 15, 2018 at 8:43
  • @sanzante – Which *.yml files are you thinking of which declare templates?
    – leymannx
    Jun 15, 2018 at 9:28

1 Answer 1


Inside js_example.module there's a function which declares how a render array using a certain '#theme' is to be rendered as HTML and which variables might be available. This function is hook_theme. It also can define a template that Drupal will look for inside the module's template folder.

function js_example_theme() {

  return [
    'js_example_accordion' => [
      'template' => 'accordion',
      'variables' => [
        'title' => NULL,

Next there is the src/Controller/JsExampleController.php which has a function to output stuff and is told to use the 'js_example_accordion'-'#theme' for that.

  public function getJsAccordionImplementation() {

    $title = $this->t('Click sections to expand or collapse:');

    $build['myelement'] = [
      '#theme' => 'js_example_accordion',
      '#title' => $title,

    $build['myelement']['#attached']['library'][] = 'js_example/js_example.accordion';

    return $build;

That's the basics.

The description.html.twig is used a little bit differently but maybe just skip that for now as its some kind of advanced exception you'll understand later.

  • In the PHP controller I found this comment: This class uses the DescriptionTemplateTrait to display text we put in the * templates/description.html.twig file. Jun 15, 2018 at 10:12
  • Yeah, search the example module for DescriptionTemplateTrait and explore what you'll find there :)
    – leymannx
    Jun 15, 2018 at 10:39
  • 1
    I analyzed the content of the trait DescriptionTemplateTrait Now it's clear :) Jun 15, 2018 at 10:56
  • Cool, then please consider to upvote and accept this answer.
    – leymannx
    Jun 15, 2018 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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