@christianwiedemann/drupal-twig-extensions

中文文档
2.0.0-beta.3发布于 2025/09/30(MIT OR GPL-2.0-only)ESM + CJS近 30 天下载 4112

JavaScript implementation of Drupal’s Twig extensions

原始使用文档
文档目录11 节
  1. Drupal’s Twig extensions for JavaScript
  2. Installation
  3. Usage
  4. Twig.js 1.15.0 or later
  5. Twing 5.1.0 or later
  6. Options for addDrupalExtensions()
  7. Implementation details
  8. Filters
  9. Functions
  10. Tags
  11. Tests and operators

drupal-twig-extensions

Drupal’s Twig extensions for JavaScript

Node.js CI Status Coverage Status

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.

Installation

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

Usage

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 }}

Twig.js 1.15.0 or later

If you use Twig.js and import statements (i.e. ES Module syntax) in your JavaScript, 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 "Options" below for
  // details.
});

If you use Twig.js and require statements (i.e. CommonJS syntax) in your JavaScript, use the following JavaScript to integrate with Twig:

const Twig = require('twig');
const { addDrupalExtensions } = require('drupal-twig-extensions/twig');

// Add the extensions for Drupal.
addDrupalExtensions(Twig, {
  // Optionally, set options to configure how the Drupal
  // extensions should behave. See "Options" below for
  // details.
});

Twing 5.1.0 or later

If you use Twing and import statements (i.e. ES Module syntax) in your JavaScript, 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 "Options" below for
  // details.
});

// If you use twing-loader, it will need access to the same
// twing environment.
export default twingEnvironment;

If you use Twing and require statements (i.e. CommonJS syntax) in your JavaScript, use the following JavaScript to integrate with Twing:

const { TwingEnvironment, TwingLoaderRelativeFilesystem } = require('twing');
const { addDrupalExtensions } = require('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 "Options" below for
  // details.
});

// If you use twing-loader, it will need access to the same
// twing environment.
module.exports = twingEnvironment;

Options for addDrupalExtensions()

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',

  // Set the base_url of the Drupal install; defaults to '/'.
  base_url: '/',

  // Override any of Drupal's default stream wrappers.
  // Or add your own stream wrappers; new stream wrappers will
  // be merged with the default ones.
  streamWrapper: {
    'public://': 'sites/default/files',
    'private://': 'sites/default/private',
    'temporary://': 'sites/default/tmp',
  },

  // Override any of Drupal's default date_format patterns.
  // Or add your own custom formats; new formats will be
  // merged with the default ones.
  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);

Implementation details

Filters

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
  • render1
  • safe_join
  • t2
  • trans2
  • without

1 The render 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.

Functions

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_library3
  • create_attribute
  • file_url
  • link
  • path2
  • render_var1
  • url2

1 See the note above about the render filter.

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 This function does not work the same as it does in Drupal; it just returns an empty string. It is implemented to keep Twig compilation from breaking when this Drupal-specific function is used in Twig templates.

Tags

NOTE: Tags have not yet been implemented in this project.

The official list of tags is in Drupal 9’s code.

Tests and operators

Drupal 9 does not currently implement any custom Twig tests or Twig operators.

版本与发布记录

@christianwiedemann/drupal-twig-extensions 共发布 3 个版本,首次发布于 2025/07/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0-beta.3latest2025/09/301.1 年前
2.0.0-beta.22025/07/271.3 年前
2.0.0-beta.12025/07/261.3 年前

关于 @christianwiedemann/drupal-twig-extensions 的常见问题

@christianwiedemann/drupal-twig-extensions 是什么?

JavaScript implementation of Drupal’s Twig extensions

如何安装 @christianwiedemann/drupal-twig-extensions?

在项目目录运行 npm install @christianwiedemann/drupal-twig-extensions,或使用 pnpm add / yarn add / bun add。

@christianwiedemann/drupal-twig-extensions 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/christianwiedemann__drupal-twig-extensions 是否存在。

@christianwiedemann/drupal-twig-extensions 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@christianwiedemann/drupal-twig-extensions 可以商用吗?

@christianwiedemann/drupal-twig-extensions 使用 (MIT OR GPL-2.0-only) 许可。可在 MIT、GPL-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@christianwiedemann/drupal-twig-extensions 的下载量有多少?

根据 npm 官方下载统计,@christianwiedemann/drupal-twig-extensions 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,112 次,日均约 137 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@christianwiedemann/drupal-twig-extensions 还在维护吗?

最新版本 2.0.0-beta.3 发布于 2025/09/30(1.1 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/07/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。