-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
53 lines (45 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var nunjucks = require("nunjucks");
var path = require("path");
module.exports = function(env, callback) {
// Load the new nunjucks environment.
var loaderOpts = {
watch: (env.mode == 'preview')
};
var loader = new nunjucks.FileSystemLoader(env.templatesPath, loaderOpts);
var nenv = new nunjucks.Environment(loader);
// Load the filters
if(env.config.nunjucks && env.config.nunjucks.filterdir) {
env.config.nunjucks.filters.map( function (name) {
file = path.join(env.config.nunjucks.filterdir, name + ".js");
filter = env.loadModule(env.resolvePath(file), true);
nenv.addFilter(name, filter);
});
}
if(env.config.nunjucks && env.config.nunjucks.filtermodules) {
env.config.nunjucks.filtermodules.map( function (name) {
var filter = require(name);
filter.install(nenv);
});
}
// Configure nunjucks environment.
if (env.config.nunjucks && env.config.nunjucks.autoescape != null) {
nenv.opts.autoescape = env.config.nunjucks.autoescape;
} else {
nenv.opts.autoescape = false;
}
var NunjucksTemplate = function(template) {
this.template = template;
};
NunjucksTemplate.prototype.render = function render(locals, callback) {
try {
callback(null, new Buffer(this.template.render(locals)));
} catch (error) {
callback(error);
}
};
NunjucksTemplate.fromFile = function fromFile(filepath, callback) {
callback(null, new NunjucksTemplate(nenv.getTemplate(filepath.relative)));
};
env.registerTemplatePlugin("**/*.*(html|nunjucks|njk)", NunjucksTemplate);
callback();
};