gulp-njrt
Render Nunjucks templates
Issues with the output should be reported on the Nunjucks
issue tracker.
Install
Install with npm
npm install --save-dev gulp-njrt
Example
var gulp = require('gulp');
var nunjucksRender = require('gulp-njrt');
gulp.task('default', function () {
return gulp.src('src/templates/*.html')
.pipe(nunjucksRender({
src: 'src/templates'
}))
.pipe(gulp.dest('dist'));
});
Note: To keep Nunjucks render from eating up all your ram, make sure to
specify the src
path(s) option. This will also allow you to define your paths
relatively.
Example with gulp data
var gulp = require('gulp');
var nunjucksRender = require('gulp-njrt');
var data = require('gulp-data');
function getDataForFile(file){
return {
example: 'data loaded for ' + file.relative
};
}
gulp.task('default', function () {
return gulp.src('src/templates/*.html')
.pipe(data(getDataForFile))
.pipe(nunjucksRender({
src: ['src/templates/']
}))
.pipe(gulp.dest('dist'));
});
API
nunjucks-render(options)
Same options as nunjucks.configure()
:
- watch (default: false) reload templates when they are changed.
- express an express app that nunjucks should install to.
- autoescape (default: false) controls if output with dangerous characters are escaped automatically.
- tags: (default: see nunjucks syntax) defines the syntax for nunjucks tags.
With the following additional options:
- extension (default: ".html") String. File extension to output.
- src (default: undefined) String or Array. Source path(s) being configured.
- data (default: {}) Ojbect. Context data available to all templates.
- globals (default: undefined) Object. Provides
filters
and functions
properties, which are are added to the nunjucks environment or context.
For example
nunjucksRender({
data: {css_path: 'http://company.com/css/'}
});
For the following template
<link rel="stylesheet" href="{{ css_path }}test.css" />
Would render
<link rel="stylesheet" href="http://company.com/css/test.css" />
Watch mode
Nunjucks' watch feature, which is normally enabled by default, is disabled by
default in this plugin. Pass watch: true
to enable it:
nunjucksRender({
src: './source',
watch: true
});
License
MIT © Devoptix LLC
Shout-outs
Carlos G. Limardo who wrote
gulp-nunjucks-render
which I am forking in order to update Nunjucks and do other stuff.
Sindre Sorhus who wrote the original
gulp-nunjucks for precompiling
Nunjucks templates. I updated his to render instead of precompile.