Twig.js is a pure JavaScript implementation of the Twig PHP templating language (http://twig.sensiolabs.org/)
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.
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: https://github.com/twigjs/twig.js/releases or via NPM:
npm install twig --save
A bower package is available from philsbury. Please direct any Bower support issues to that repo.
Include twig.js or twig.min.js in your page, then:
var template = twig({
data: 'The {{ baked_good }} is a lie.'
});
console.log(
template.render({baked_good: 'cupcake'})
);
// outputs: "The cupcake is a lie."
A loader is available from zimmo.be.
Tested on node >=6.0.
You can use twig in your app with
var Twig = require('twig'), // Twig module
twig = Twig.twig; // Render function
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
});
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
< 847A div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var Twig = require("twig"), express = require('express'), app = express(); // This section is optional and used to configure twig. app.set("twig options", { allow_async: true, // Allow asynchronous compiling strict_variables: false }); app.get('/', function(req, res){ res.render('index.twig', { message : "Hello World" }); }); app.listen(9999);">var Twig = require("twig"), express = require('express'), app = express(); // This section is optional and used to configure twig. app.set("twig options", { allow_async: true, // Allow asynchronous compiling strict_variables: false }); app.get('/', function(req, res){ res.render('index.twig', { message : "Hello World" }); }); app.listen(9999);