Is the glue for express and nunjucks.
- Easy connection.
- Uses of common templates, filters and extensions.
- Uses an asynchronous loader templates nunjucks-async-loader.
- Support context processors.
$ npm install nunjucks express-nunjucks --save
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const app = express();
const isDev = app.get('env') === 'development';
app.set('views', __dirname + '/templates');
const njk = expressNunjucks(app, {
watch: isDev,
noCache: isDev
});
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
Express application or an array of applications.
watch=false {Boolean}
- if true, the system will automatically update templates when they are changed on the filesystem.noCache=false {Boolean}
- if true, the system will avoid using a cache and templates will be recompiled every single time.autoescape=true {Boolean}
- controls if output with dangerous characters are escaped automatically.throwOnUndefined=false {Boolean}
- throw errors when outputting a null/undefined value.trimBlocks=false {Boolean}
- automatically remove trailing newlines from a block/tag.lstripBlocks=false {Boolean}
- automatically remove leading whitespace from a block/tag.tags
- defines the syntax for nunjucks tags.filters
- defines the syntax for nunjucks filters.loader
- defines loader templates. The default is the asynchronous loader templates.
Creates Express middleware to work context processors.
Returns Nunjucks Environment.
Create custom filters in nunjucks.
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const filters = require('./filters');
const app = express();
app.set('views', __dirname + '/templates');
const njk = expressNunjucks(app, {
// Add custom filter.
filters: filters
});
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
Context processors is one great idea from the django framework.
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const webpackAssets = require('./build/assets');
const app = express();
app.set('views', __dirname + '/templates');
// Adds information about the request in the context of the template.
const reqCtxProcessor = (req, ctx) => {
ctx.req = req;
};
// Adds links to statics in the context of the template.
const assetsCtxProcessor = (req, ctx) => {
ctx.scripts = webpackAssets.scripts;
ctx.styles = webpackAssets.styles;
};
const njk = expressNunjucks(app);
app.use(njk.ctxProc([
reqCtxProcessor,
assetsCtxProcessor
]));
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const app = express();
app.set('views', __dirname + '/templates');
const njk = expressNunjucks(app, {
loader: nunjucks.FileSystemLoader
});
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000);
// proj/app.js
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const subApp = require('./subapp');
const app = express();
app.set('views', __dirname + '/templates');
const njk = expressNunjucks([app, subApp]);
app.get('/', (req, res) => {
res.render('index');
});
app.use('/subApp', subApp);
// and more...
app.listen(3000);
// proj/subapp/index.js
const express = require('express');
const app = express();
app.set('views', __dirname + '/templates');
app.get('/', (req, res) => {
res.render('index');
});
module.exports = app;
proj
|
|- templates
| |
| |- base.html
| |- index.html
| |-subapp
| |
| |-page.html
|
|- subapp
|
|-templates
|
|-subapp
|
|-index.html
|-page.html
The templates in the directory proj/templates/subapp
override templates proj/subapp/templates/subapp
.
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test