JS port of the Twig templating language.
npm install twig



title="Twig.js"
src="https://user-images.githubusercontent.com/3282350/29336704-ab1be05c-81dc-11e7-92e5-cf11cca7b344.png">
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.
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:
``bash`
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:
`js
var template = Twig.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
`js`
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:
`js`
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
`js
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);
`
`html`
Message of the moment: {{ message }}
An Express 2 Example is available on the wiki.
- Twing
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.
The twig.js tests are written in [Mocha][mocha] and can be invoked with npm test`.
Twig.js is available under a [BSD 2-Clause License][bsd-2], see the LICENSE file for more information.
See the LICENSES.md file for copies of the referenced licenses.
1. The JavaScript Array fills in src/twig.fills.js are from
2. The Date.format function in src/twig.lib.js is from
3. The sprintf implementation in src/twig.lib.js used for the format filter is from
4. The strip_tags implementation in src/twig.lib.js used for the striptags filter is from
[mit-jpaq]: http://jpaq.org/license/
[mit-phpjs]: http://phpjs.org/pages/license/#MIT
[mit]: http://www.opensource.org/licenses/mit-license.php
[mdn-license]: https://developer.mozilla.org/Project:Copyrights
[bsd-2]: http://www.opensource.org/licenses/BSD-2-Clause
[bsd-3]: http://www.opensource.org/licenses/BSD-3-Clause
[cc-by-sa-2.5]: http://creativecommons.org/licenses/by-sa/2.5/ "Creative Commons Attribution-ShareAlike 2.5 License"
[mocha]: http://mochajs.org/
[qunit]: http://docs.jquery.com/QUnit