Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/avcs06/gulp-nunjucks-with-env

Use Nunjuck's Environment Class to get more control
https://github.com/avcs06/gulp-nunjucks-with-env

Last synced: 15 days ago
JSON representation

Use Nunjuck's Environment Class to get more control

Awesome Lists containing this project

README

        

# [gulp-nunjucks-with-env](https://github.com/avcs06/gulp-nunjucks-with-env)
> Use [Nunjucks](http://jlongster.github.io/nunjucks/) Environment Class to get more control

*Issues with the output should be reported on the Nunjucks [issue tracker](https://github.com/jlongster/nunjucks/issues).*

## Install

Install with [npm](https://npmjs.org/package/gulp-nunjucks-with-env)

```
npm install --save-dev gulp-nunjucks-with-env
```

## Example : Using Environment Class

```js
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](https://mozilla.github.io/nunjucks/api.html#environment) 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

```js
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

```js
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`](https://mozilla.github.io/nunjucks/api.html#environment)

context (optional) `Object` :
Same context as [`nunjucks.render()`](http://jlongster.github.io/nunjucks/api.html#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
```

```

Would render
```

```

## License

MIT © [AvcS](http://www.avcs-tips.com)

## Shout-outs

[Sindre Sorhus](http://sindresorhus.com/) who wrote the original [gulp-nunjucks](https://www.npmjs.org/package/gulp-nunjucks) for precompiling Nunjucks templates.

[Carlos G. Limardo](http://limardo.org) updated this to render instead of precompile in [gulp-nunjucks-render](https://www.npmjs.org/package/gulp-nunjucks-render)

I updated this to facilitate using of Environment Class for more control