twig
JS port of the Twig templating language.
Last updated 9 years ago by dave-irvine .
BSD-2-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ npm install twig 
SYNC missed versions from official npm registry.

Known Vulnerabilities Build Status NPM version Gitter

About

Twig.js is a pure JavaScript implementation of the Twig PHP templating language (https://twig.symfony.com/)

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).

Docs

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.

Install

Download the latest twig.js release from github: https://github.com/twigjs/twig.js/releases or via NPM:

npm install twig --save

Bower

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.'
});

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

Webpack

A loader is available from zimmo.be.

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.

app.js

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"
  });
});

app.listen(9999);

views/index.twig

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

An Express 2 Example is available on the wiki.

Alternatives

Contributing

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.

Tests

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

License

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

Acknowledgments

See the LICENSES.md file for copies of the referenced licenses.

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

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

  3. The sprintf implementation in src/twig.lib.js used for the format filter is from http://www.diveintojavascript.com/projects/javascript-sprintf 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 http://phpjs.org/functions/strip_tags and used under and MIT License.

Current Tags

  • 1.17.1                                ...           latest (a year ago)

63 Versions

  • 1.17.1                                ...           a year ago
  • 1.17.0                                ...           a year ago
  • 1.16.0                                ...           2 years ago
  • 1.15.4                                ...           4 years ago
  • 1.15.3                                ...           4 years ago
  • 1.15.2                                ...           4 years ago
  • 1.15.1                                ...           5 years ago
  • 1.15.0                                ...           5 years ago
  • 1.14.0                                ...           5 years ago
  • 1.13.3                                ...           6 years ago
  • 1.13.2                                ...           6 years ago
  • 1.13.1                                ...           6 years ago
  • 1.13.0                                ...           6 years ago
  • 1.12.0                                ...           6 years ago
  • 1.11.1                                ...           6 years ago
  • 1.11.0                                ...           7 years ago
  • 1.10.5                                ...           7 years ago
  • 1.10.4                                ...           8 years ago
  • 0.10.3                                ...           8 years ago
  • 0.10.2                                ...           8 years ago
  • 0.10.1                                ...           8 years ago
  • 0.10.0                                ...           8 years ago
  • 0.9.5                                ...           9 years ago
  • 0.9.4                                ...           9 years ago
  • 0.9.3                                ...           9 years ago
  • 0.9.2                                ...           9 years ago
  • 0.9.1                                ...           9 years ago
  • 0.8.9                                ...           9 years ago
  • 0.8.8                                ...           9 years ago
  • 0.8.7                                ...           9 years ago
  • 0.8.6                                ...           9 years ago
  • 0.8.5                                ...           9 years ago
  • 0.8.2                                ...           10 years ago
  • 0.8.1                                ...           10 years ago
  • 0.8.0                                ...           10 years ago
  • 0.7.2                                ...           10 years ago
  • 0.7.1                                ...           11 years ago
  • 0.7.0                                ...           11 years ago
  • 0.6.0                                ...           11 years ago
  • 0.5.13                                ...           11 years ago
  • 0.5.12                                ...           11 years ago
  • 0.5.11                                ...           11 years ago
  • 0.5.10                                ...           11 years ago
  • 0.5.9                                ...           11 years ago
  • 0.5.8                                ...           12 years ago
  • 0.5.7                                ...           12 years ago
  • 0.5.6                                ...           12 years ago
  • 0.5.5                                ...           12 years ago
  • 0.5.4                                ...           12 years ago
  • 0.5.3                                ...           12 years ago
  • 0.5.2                                ...           12 years ago
  • 0.5.1                                ...           12 years ago
  • 0.5.0                                ...           12 years ago
  • 0.4.6                                ...           12 years ago
  • 0.4.5                                ...           12 years ago
  • 0.4.4                                ...           12 years ago
  • 0.4.3                                ...           12 years ago
  • 0.4.2                                ...           13 years ago
  • 0.4.1                                ...           13 years ago
  • 0.4.0                                ...           13 years ago
  • 0.3.0                                ...           13 years ago
  • 0.2.0                                ...           13 years ago
  • 0.1.0                                ...           13 years ago
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (8)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE