Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Twig.js

86 sites Security covered
View on drupal.org

This module provides the twig.js JavaScript library, allowing you to render Twig templates directly in the browser. It also offers a "light" version that uses underscore.js for simpler templating needs, reducing JavaScript overhead.

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
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Release date
2.0.4 Stable Feb 10, 2025