Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Twig.js

93 sites Security covered Drupal 8–11
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.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Feb 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Core Release date
2.0.4 Stable 8–11 Feb 10, 2025