Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

twigjs

92 sites Security covered
View on drupal.org

This module provides twig.js and is intended for developers or if some other module requires it.

Usage

Often you would probably render first something server side, and so you want to store the template in a variable. So for example you can do this, like from the tests for this module:

$template = '<ul>{% for user in users %}
                  <li>{{ user }}</li>
                 {% endfor %}
             </ul>';
return [
  '#type' => 'inline_template',
  '#prefix' => '<div id="twigjs_test_js">',
  '#suffix' => '</div>',
  '#template' => $template,
  '#context' => [
    'users' => $some_user_data,
  ],
  '#attached' => [
    'drupalSettings' => [
      'twigjsTest' => [
        'inlineTemplate' => $template,
      ],
      'library' => [
        // To include things like the "trans" tag.
        'twigjs/drupal.twigjs',
        // ...or to just include the twig.js library.
        'twigjs/twigjs',
      ],
    ],
  ],
];

Then, in JavaScript:

  Drupal.behaviors.twigJsTestSimple = {
    attach: function(context) {
      // Get the element we want to render to.
      var wrapper = document.getElementById('twigjs_test_js');
      // This is twig.js, you can read the documentation at https://github.com/twigjs/twig.js
      var template = Twig.twig({
        id: 'twigjs_test',
        data: settings.twigjsTest.inlineTemplate
      });
      wrapper.innerHTML = template.render({users: ['testUser']});
    }
  };

"Light" version

This module also exposes a "light" version, that is based on underscore.js, which is in core. This version has its limitations. For example you can not use any twig tags, which is a pretty big limitation.

A reason to use this is the amount of kilobytes of JavaScript you want to include just to be able to use something "twig-like". For example, if all you need is to render one template on both the server and the client, and the template is quite simple, you might want to go with the "light" version.

You can read more about underscore's template function here.

This module exposes this with the same API as the regular twig.js, only under the namespace "TwigLight":

// This is from the tests of this module:
var lightWrapper = document.getElementById('twigjs-test-light-wrapper-js');
if (lightWrapper) {
  var template = TwigLight.twig({
    id: 'light',
    // This particular template is like this: 
    // 'I have a variable called name and it is {{ name }} and I have a number and it is {{ number }}';
    data: settings.twigjsTest.lightTemplate
  });
  lightWrapper.innerHTML = template.render({
    name: 'testName',
    number: 1337
  });
}

Disclaimers

There is not a 1:1 relationship between twig in Drupal and twig.js. For example, you can't use a template like this, with a nested variable:

<div class"content">
  {{ content }}
</div>

And then a variable like this:

$build['content'] = [
  'image' => $image_render_array,
  'text' => $text_render_array,
];

For twig.js, the variables needs to be strings. Be aware of this.

Activity

Total releases
1
First release
Feb 2025
Latest release
1 year ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
2.0.4 Stable Feb 10, 2025