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 server side JavaScript environment such as deno.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/LangiviTechnology/twig.js or via deno.land:
import Twig, {renderToString} from 'https://deno.land/x/twig@1.16.2/mod.js'
Twig is compatible with Opine. You can create an opine app using the twig.js templating language by setting the view engine to twig.
opine 2.1.3
import opine from "https://deno.land/x/opine@2.1.3/mod.ts";
import {renderToString} from "../../src/twig.js";
const app = opine();
//specifies a function to process the template
app.engine('twig', renderToString);
//specify the folder with templates
app.set('views',"./view");
app.set('view engine', 'twig');
app.set('view cache', true);
app.get('/',function(req,res){
res.render("index.twig", {
message : "Hello World"
});
});
app.listen(3001);
Message of the moment: <b>{{ message }}</b>
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
.
See the LICENSES.md file for copies of the referenced licenses.
-
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.
-
The Date.format function in src/TwigLib.js is from http://jpaq.org/ and used under a MIT license.
-
The sprintf implementation in src/TwigLib.js used for the format filter is from http://www.diveintojavascript.com/projects/javascript-sprintf and used under a BSD 3-Clause License.
-
The strip_tags implementation in src/TwigLib.js used for the striptags filter is from http://phpjs.org/functions/strip_tags and used under and MIT License.