DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/twig package

1.17.1 • Public • Published

Known Vulnerabilities Build Status NPM version Gitter


Twig.js is a pure JavaScript implementation of the Twig PHP templating language (

The goal is to provide a library that is compatible with both browsers and server side JavaScript environments such as node.js.

Twig.js is currently a work in progress and supports a limited subset of the Twig templating language (with more coming).


Documentation is available in the twig.js wiki on Github.

Feature Support

For a list of supported tags/filters/functions/tests see the Implementation Notes page on the wiki.


Download the latest twig.js release from github: or via NPM:

npm install twig --save


A bower package is available from philsbury. Please direct any Bower support issues to that repo.

Browser Usage

Include twig.js or twig.min.js in your page, then:

var template = Twig.twig({
    data: 'The {{ baked_good }} is a lie.'

    template.render({baked_good: 'cupcake'})
// outputs: "The cupcake is a lie."


A loader is available from

Node Usage (npm)

Tested on node >=6.0.

You can use twig in your app with

var Twig = require('twig'), // Twig module
    twig = Twig.twig;       // Render function

Usage without Express

If you don't want to use Express, you can render a template with the following method:

import Twig from 'twig';
Twig.renderFile('./path/to/someFile.twig', {foo:'bar'}, (err, html) => {
  html; // compiled string

Usage with Express

Twig is compatible with express 2 and 3. You can create an express app using the twig.js templating language by setting the view engine to twig.


Express 3

var Twig = require("twig"),
    express = require('express'),
    app = express();

// This section is optional and used to configure twig.
app.set("twig options", {
    allowAsync: true, // Allow asynchronous compiling
    strict_variables: false

app.get('/', function(req, res){
  res.render('index.twig', {
    message : "Hello World"



Message of the moment: <b>{{ message }}</b>

An Express 2 Example is available on the wiki.



If you have a change you want to make to twig.js, feel free to fork this repository and submit a pull request on Github. The source files are located in src/*.js.

twig.js is built by running npm run build

For more details on getting setup, see the contributing page on the wiki.

Environment Requirements

When developing on Windows, the repository must be checked out without automatic conversion of LF to CRLF. Failure to do so will cause tests that would otherwise pass on Linux or Mac to fail instead.


The twig.js tests are written in Mocha and can be invoked with npm test.


Twig.js is available under a BSD 2-Clause License, see the LICENSE file for more information.


See the file for copies of the referenced licenses.

  1. The JavaScript Array fills in src/twig.fills.js are from and are available under the MIT License or are public domain.

  2. The Date.format function in src/twig.lib.js is from and used under a MIT license.

  3. The sprintf implementation in src/twig.lib.js used for the format filter is from and used under a BSD 3-Clause License.

  4. The strip_tags implementation in src/twig.lib.js used for the striptags filter is from and used under and MIT License.




Package Sidebar


npm i twig

Weekly Downloads






Unpacked Size

1.17 MB

Total Files


Last publish


  • dave-irvine
  • justjohn
  • robloach
  • justafish
  • plepe
  • evgenius
  • willrowe