What is twig?
The 'twig' npm package is a JavaScript implementation of the Twig templating engine, which is originally a PHP-based template engine. It allows developers to create dynamic and reusable templates for web applications. Twig is known for its simplicity, flexibility, and performance.
What are twig's main functionalities?
Template Rendering
This feature allows you to define and render templates with dynamic data. The example shows how to create a simple template and render it with a variable.
const Twig = require('twig');
// Define a template
const template = Twig.twig({
data: 'Hello, {{ name }}!'
});
// Render the template with data
const output = template.render({ name: 'World' });
console.log(output); // Output: Hello, World!
Template Inheritance
Template inheritance allows you to create a base template and extend it in child templates. This promotes reusability and maintainability of your templates. The example demonstrates how to extend a base template and render it with dynamic content.
const Twig = require('twig');
// Define a base template
const baseTemplate = Twig.twig({
id: 'base',
data: '<html><body>{% block content %}{% endblock %}</body></html>'
});
// Define a child template that extends the base template
const childTemplate = Twig.twig({
data: '{% extends "base" %}{% block content %}Hello, {{ name }}!{% endblock %}'
});
// Render the child template with data
const output = childTemplate.render({ name: 'World' });
console.log(output); // Output: <html><body>Hello, World!</body></html>
Custom Filters
Custom filters allow you to extend Twig's functionality by defining your own filters. The example shows how to create a custom 'capitalize' filter and use it in a template.
const Twig = require('twig');
// Define a custom filter
Twig.extendFilter('capitalize', function(value) {
return value.charAt(0).toUpperCase() + value.slice(1);
});
// Define a template using the custom filter
const template = Twig.twig({
data: 'Hello, {{ name|capitalize }}!'
});
// Render the template with data
const output = template.render({ name: 'world' });
console.log(output); // Output: Hello, World!
Other packages similar to twig
nunjucks
Nunjucks is a powerful templating engine for JavaScript, inspired by Jinja2 (Python) and Twig (PHP). It offers similar features such as template inheritance, custom filters, and asynchronous rendering. Nunjucks is known for its flexibility and ease of use, making it a popular choice for JavaScript developers.
handlebars
Handlebars is a popular templating engine that provides a simple and intuitive syntax for creating dynamic templates. It supports features like template partials, helpers, and custom expressions. Handlebars is known for its performance and is widely used in both client-side and server-side applications.
ejs
EJS (Embedded JavaScript) is a templating engine that allows you to generate HTML markup with plain JavaScript. It supports features like template partials, custom filters, and includes. EJS is known for its simplicity and ease of integration with Express.js and other Node.js frameworks.
About
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).
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.
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 make
For more details on getting setup, see the contributing page on the wiki.
Browser Usage
Twig.js can be installed as a bower package with:
bower install twig.js
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'})
);
Node Usage
Twig.js can be installed with NPM
npm install twig
You can use twig in your app with
var Twig = require('twig'), // Twig module
twig = Twig.twig; // Render function
Twig is compatable 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();
app.set("twig options", {
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.
Tests
The twig.js tests are written in Mocha and can be invoked with make 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.
-
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/twig.lib.js is from http://jpaq.org/ and used under a MIT license.
-
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.
-
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.