What is nunjucks?
Nunjucks is a powerful templating engine for JavaScript, inspired by Jinja2. It is designed to be fast, extendable, and easy to use, making it suitable for both server-side and client-side applications.
What are nunjucks's main functionalities?
Template Rendering
Nunjucks allows you to render templates with dynamic data. In this example, the template 'Hello {{ name }}!' is rendered with the context { name: 'World' }, resulting in 'Hello World!'.
const nunjucks = require('nunjucks');
nunjucks.configure({ autoescape: true });
const renderedString = nunjucks.renderString('Hello {{ name }}!', { name: 'World' });
console.log(renderedString);
Template Inheritance
Nunjucks supports template inheritance, allowing you to create a base template and extend it in other templates. This example shows a base template 'base.html' and an extending template 'index.html'. The rendered output will have the title 'Home' and the content 'Welcome to the homepage!'.
const nunjucks = require('nunjucks');
nunjucks.configure('views');
// base.html
// <html>
// <head><title>{% block title %}Default Title{% endblock %}</title></head>
// <body>{% block content %}{% endblock %}</body>
// </html>
// index.html
// {% extends 'base.html' %}
// {% block title %}Home{% endblock %}
// {% block content %}Welcome to the homepage!{% endblock %}
const renderedString = nunjucks.render('index.html');
console.log(renderedString);
Custom Filters
Nunjucks allows you to create custom filters to manipulate data within templates. In this example, a custom filter 'shorten' is created to truncate a string to a specified length. The template 'Result: {{ message | shorten(7) }}' will render as 'Result: Hello, ' when provided with the context { message: 'Hello, World!' }.
const nunjucks = require('nunjucks');
nunjucks.configure();
nunjucks.addFilter('shorten', function(str, count) {
return str.slice(0, count || 5);
});
const renderedString = nunjucks.renderString('Result: {{ message | shorten(7) }}', { message: 'Hello, World!' });
console.log(renderedString);
Other packages similar to nunjucks
handlebars
Handlebars is another popular templating engine for JavaScript. It uses a similar syntax to Nunjucks but is more focused on simplicity and logic-less templates. Handlebars is known for its ease of use and integration with various frameworks.
ejs
EJS (Embedded JavaScript) is a templating engine that allows you to generate HTML with plain JavaScript. It is less feature-rich compared to Nunjucks but is very straightforward and easy to use, making it a good choice for simple templating needs.
pug
Pug, formerly known as Jade, is a high-performance template engine heavily influenced by Haml. It uses indentation-based syntax, which can be more concise but also has a steeper learning curve compared to Nunjucks. Pug is known for its clean and readable templates.
h1. Nunjucks
I'm tired of the lack of good templating systems for large js apps. The biggest missing feature between all of them is template inheritance, something necessary for a non-trivial site. Other problems is the ugliness of embedding javascript, too much logic in templates, etc.
I hope that Nunjucks can be better. It is heavily jinja2 inspired because I think it is a great solution for templating. Nunjucks does not claim to be a direct port of jinja2, but most things will work the same in both projects.
h2. Installation
bc. npm install nunjucks
h2. Features
- variables
- jinja2-style parsing @{{ foo('bar', 1) }}@
- conditionals
- iteration
- array/dict lookups
- template inheritance using @extends@ and @block@ tags
- include other templates with @include@
- ignore template parsing with @raw@ tag
- very fast and active development!
h2. Express
To integrate with express, do the following (this will change before the first release):
bc.. var nunjucks = require('nunjucks');
var loaders = nunjucks.loaders;
var express = require('express');
var env = new nunjucks.Environment(new loaders.FileSystemLoader('templates'));
env.express(app);
h2. Using
Here is an example template that works with the current code:
bc.. --- base.html
{{ title }}
{% block content %}{% endblock %}
--- index.html
{% extends "base.html" %}
{% block content %}
{# Variables and filters #}
Hello {{ name|title }}!
{# Call a function #}
{{ messages() }}
{# Conditionals #}
{% if sick %}
no
{% elif throwing %}
THROWING UP?!
{% elif pooping %}
STOP POOPING
{% else %}
GOOD!
{% endif %}
{# For iteration #}
Let's print some numbers:
{% endblock %}
p. To compile templates, you can create a Template directly or load one from an environment object. Right now the environment just loads the template from the disk relative to the current location.
bc.. var nunjucks = require('nunjucks');
var tmpl = new nunjucks.Template('Hello {{ username }}');
console.log(tmpl.render({ username: "james" }));
// From an environment
var env = new nunjucks.Environment();
var tmpl = env.getTemplate('test.html');
console.log(tmpl.render({ username: "james" }));
h2. Status
All of the features that will be in v0.1 have been ported over. I am currently testing the codebase and letting it solidify before I make the first release. The focus is now documentation, benchmarks, and tests.
Features needed for v0.1:
- Better express integration
- Builtin filters
- Thorough-ish documentation, especially what features are missing
- Benchmarks
Features needed for v0.2:
- Client-side js support (load precompiled templates, convert modules, etc)
- Macros, a few other missing jinja2 constructs
- Even more thorough documentation