gulp-nunjucks-with-env
v1.0.2
Published
Use Nunjucks Environment Class to get more control
Downloads
5
Maintainers
Readme
gulp-nunjucks-with-env
Use Nunjucks Environment Class to get more control
Issues with the output should be reported on the Nunjucks issue tracker.
Install
Install with npm
npm install --save-dev gulp-nunjucks-with-env
Example : Using Environment Class
var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-with-env');
var environment = nunjucksRender.nunjucks.configure(['src/templates/']);
environment.addFilter('stringify', function(obj) {
return JSON.stringify(obj);
});
nunjucksRender = nunjucksRender.bind(nunjucksRender,environment);
gulp.task('default', function () {
return gulp.src('src/templates/*.html')
.pipe(nunjucksRender())
.pipe(gulp.dest('dist'));
});
Check Nunjucks API for more information on Environment Class
Note: To keep Nunjucks render from eating up all your ram, make sure to specify your watch path. nunjucksRender.nunjucks.configure(['src/path/to/templates/']);
This will also allow you to define your paths relatively.
Example : Using with gulp data
var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-render');
var data = require('gulp-data');
function getDataForFile(file){
return {
example: 'data loaded for ' + file.relative
};
}
gulp.task('default', function () {
nunjucksRender.nunjucks.configure(['src/templates/']);
return gulp.src('src/templates/*.html')
.pipe(data(getDataForFile))
.pipe(nunjucksRender())
.pipe(gulp.dest('dist'));
});
Example : Using with pass by reference globals
var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-with-env');
var environment = nunjucksRender.nunjucks.configure(['src/templates/']);
environment.addFilter('stringify', function(obj) {
return JSON.stringify(obj);
});
var files = {widgetFiles : []};
templateEngine = templateEngine.bind(templateEngine,environment,{},files);
gulp.task('task', function () {
return gulp.src('src/templates/*.html')
.pipe(nunjucksRender())
.pipe(gulp.dest('dist'));
});
gulp.task('newtask',['task'],function() {
return gulp.src(files.widgetFiles)
.pipe(/*Some Task*/)
});
API
nunjucksRender(environment, context, passByReferenceGlobals)
environment (optional) Environment Class
:
Environment Class
context (optional) Object
:
Same context as nunjucks.render()
Note : You will have to pass empty object {}, if you want to use passByReferenceGlobals
passByReferenceGlobals (optional) Object
:
Parameters from this object will be added to context through pass by reference rather than pass by value
Example :
nunjucksRender(environment,{css_path: 'http://company.com/css/'});
or
nunjucksRender({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" />
License
MIT © AvcS
Shout-outs
Sindre Sorhus who wrote the original gulp-nunjucks for precompiling Nunjucks templates.
Carlos G. Limardo updated this to render instead of precompile in gulp-nunjucks-render
I updated this to facilitate using of Environment Class for more control