fly-nunjucks-render
Advanced tools
Comparing version 0.0.0 to 1.0.0
54
index.js
@@ -9,34 +9,40 @@ 'use strict'; | ||
var defaults = { | ||
data: {}, | ||
dataPath: null, | ||
watch: false | ||
base: '.', | ||
data: {}, | ||
dataPath: null | ||
}; | ||
module.exports = function () { | ||
var self = this; | ||
self.filter('nunjucks', function (source, options) { | ||
options = assign({}, defaults, options || {}); | ||
return self.defer(compile.bind(self))(options); | ||
}); | ||
var self = this; | ||
self.filter('nunjucks', function (source, options) { | ||
options = assign({}, defaults, options || {}); | ||
return self.defer(compile.bind(self))(source, options); | ||
}); | ||
}; | ||
function compile(options, cb) { | ||
var self = this; | ||
function compile(src, opts, cb) { | ||
var self = this; | ||
var env = nunjucks.configure(options); | ||
// setup env | ||
nunjucks.configure(opts.base, opts); | ||
// check for a dataPath first, else resort to `options.data` | ||
var data = options.dataPath ? getData(options.dataPath) : options.data; | ||
// check for a dataPath first, else resort to `options.data` | ||
var data = opts.dataPath ? getData(self.root, opts.dataPath) : opts.data; | ||
return self.unwrap(function (files) { | ||
env.render(files, data, function (err, buf) { | ||
if (err) { | ||
return self.emit('plugin_error', { | ||
plugin: 'fly-nunjucks', | ||
error: err.message | ||
}); | ||
} | ||
cb(null, buf.toString()); | ||
}); | ||
}) | ||
// render single file | ||
return nunjucks.renderString(src.toString(), data, function (err, buf) { | ||
if (err) { | ||
return self.emit('plugin_error', { | ||
plugin: 'fly-nunjucks-render', | ||
error: err.message | ||
}); | ||
} | ||
cb(null, buf.toString()); | ||
}); | ||
} | ||
function getData(dir, file) { | ||
file = path.resolve(dir, file); | ||
return JSON.parse(fs.readFileSync(file, 'utf8')); | ||
} |
{ | ||
"name": "fly-nunjucks-render", | ||
"version": "0.0.0", | ||
"description": "Use the Nunjucks templating engine with Fly!", | ||
"version": "1.0.0", | ||
"description": "Render Nunjucks templates with Fly!", | ||
"license": "MIT", | ||
"repository": "https://github.com/lukeed/fly-nunjucks", | ||
"repository": "https://github.com/lukeed/fly-nunjucks-render", | ||
"main": "index.js", | ||
@@ -14,11 +14,9 @@ "files": [ | ||
"fly-plugin", | ||
"nunjucks" | ||
"nunjucks", | ||
"template", | ||
"html" | ||
], | ||
"scripts": { | ||
"setup": "npm i && npm test", | ||
"spec": "npm run test | tspec", | ||
"test": "npm run tape", | ||
"build": "echo No build task specified.", | ||
"deploy": "npm run test && git push origin master && npm publish", | ||
"tape": "node --harmony --harmony_arrow_functions ./node_modules/tape/bin/tape test/*.js" | ||
"test": "xo", | ||
"build": "echo No build task specified." | ||
}, | ||
@@ -35,4 +33,3 @@ "author": { | ||
"devDependencies": { | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.2.2" | ||
"xo": "*" | ||
}, | ||
@@ -39,0 +36,0 @@ "engines": { |
@@ -24,3 +24,2 @@ <div align="center"> | ||
### Example | ||
```js | ||
@@ -30,2 +29,3 @@ export default function* () { | ||
.nunjucks({ | ||
base: 'src/partials', | ||
data: {name: 'Luke Edwards'}, | ||
@@ -39,2 +39,29 @@ // or | ||
```html | ||
<p>Hello, {{ name }}!</p> | ||
``` | ||
## API | ||
#### base | ||
Type: `string`<br> | ||
Default: *`.`, the current working directory* | ||
Tells _nunjucks_ where your templates live. | ||
#### data | ||
Type: `object`<br> | ||
Default: *`{}`, optional* | ||
Context data that are passed to your view templates. | ||
#### dataPath | ||
Type: `string`<br> | ||
Default: *`null`, optional* | ||
Same as `options.data`, but parses JSON from an external file. Useful for large datasets. | ||
## License | ||
@@ -41,0 +68,0 @@ |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4901
1
38
1
77