It’s common for Drupal developers to use a style guide tool to document/display a project’s frontend components. Most of these style guide tools are written in JavaScript and don’t easily integrate with the PHP code in Twig. Fortunately, there are two different ports of Twig into JavaScript, Twig.js and Twing.
This project is the JavaScript port of Drupal 9’s Twig extensions for use with either Twig.js or Twing.
In order for this project to work, you must install either Twig.js or Twing.
To install Twig.js: npm install --save-dev twig
To install Twing: npm install --save-dev twing
Then install this project with:
npm install --save-dev drupal-twig-extensions
In your Twig templates, just follow the integration code below and you'll be able to use Drupal extensions like this:
{{ 'Hello World!'|drupal_escape }}
If you use Twig.js, use the following JavaScript to integrate with Twig:
import Twig from 'twig';
import { addDrupalExtensions } from 'drupal-twig-extensions/twig';
// Add the extensions for Drupal.
addDrupalExtensions(Twig, {
// Optionally, set options to configure how the Drupal
// extensions should behave. See below for details.
});
If you use Twing, use the following JavaScript to integrate with Twing:
import { TwingEnvironment, TwingLoaderRelativeFilesystem } from 'twing';
import { addDrupalExtensions } from 'drupal-twig-extensions/twing';
// Create an instance of the Twing Environment.
const twingEnvironment = new TwingEnvironment(
new TwingLoaderRelativeFilesystem(),
);
// Add the extensions for Drupal.
addDrupalExtensions(twingEnvironment, {
// Optionally, set options to configure how the Drupal
// extensions should behave. See below for details.
});
// If you use twing-loader, it will need access to the same twing environment.
export default twingEnvironment;
Normally, these extensions would get their configuration from Drupal itself. Since this is a JavaScript environment, you'll need to provide some of that configuration yourself; just add the specified properties to the second argument of addDrupalExtensions
.
addDrupalExtensions(twigOrTwing, {
// Set the active theme's machine name; defaults to 'stark'.
active_theme: 'zen',
// Set the active theme's path. Uses the active_theme name to
// determine the default value. Core themes default to
// 'core/themes/[active_theme]'. Other themes default to
// 'themes/custom/[active_theme]'.
active_theme_path: 'themes/contrib/zen',
// Override any of Drupal's default date_format patterns.
// Or add your own custom formats.
date_format: {
long: 'Y-m-d H:i:s',
},
});
If you don't want to specify any configuration options, you can skip the second argument. Like so:
addDrupalExtensions(twigOrTwing);
The official list of filters is in Drupal 9’s code and are described in Drupal’s Twig filters documentation.
clean_class
clean_id
drupal_escape
format_date
placeholder
render
1safe_join
t
2trans
2without
1 This filter does not work the same as it does in Drupal; it just returns the input cast to a string using its toString
method. It is implemented to keep Twig compilation from breaking when this Drupal-specific filter is used in Twig templates.
2 These filters do not work the same as they do in Drupal; they just return their input without modification. They are implemented to keep Twig compilation from breaking when these Drupal-specific filters are used in Twig templates.
The official list of functions is in Drupal 9’s code and are described in Drupal’s Twig functions documentation.
active_theme
active_theme_path
attach_library
3create_attribute
file_url
2link
path
2render_var
1url
2
1 This function does not work the same as it does in Drupal; it just returns the input cast to a string using its toString
method. It is implemented to keep Twig compilation from breaking when this Drupal-specific function is used in Twig templates.
2 These functions do not work the same as they do in Drupal; they just return their input without modification. They are implemented to keep Twig compilation from breaking when these Drupal-specific functions are used in Twig templates.
3 These functions do not work the same as they do in Drupal; they just return an empty string. They are implemented to keep Twig compilation from breaking when these Drupal-specific functions are used in Twig templates.
NOTE: Tags have not yet been implemented in this project.
The official list of tags is in Drupal 9’s code.
trans
,plural
, andendtrans
: These tags are described in Drupal’s “Translation in Twig templates” documentation.
Drupal 9 does not currently implement any custom Twig tests or Twig operators.