-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (119 loc) · 2.61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
/**
* Module dependences.
*/
var debug = require('debug')('koa:swig');
var mixin = require('utils-merge');
var path = require('path');
var swig = require('swig');
var extname = path.extname;
var resolve = path.resolve;
/**
* Expose `render`, `swig`.
*/
exports = module.exports = renderer;
exports.swig = swig;
/**
* Default render settings.
*/
var defaultSettings = {
autoescape: true,
root: 'views',
cache: 'memory',
ext: 'html',
writeBody: true
/*
locals: {},
filters: {}.
tags: {},
extensions: {}
*/
};
// Generator `renderFile`
function renderFile(pathName, locals) {
return function (done) {
swig.renderFile(pathName, locals, done);
};
}
function renderer(settings) {
// merge default settings
var sets = Object.create(defaultSettings);
// merge settings
if (settings) {
mixin(sets, settings);
}
settings = sets;
var root = settings.root;
var locals = settings.locals;
var cache = settings.cache;
var defaults = {
autoescape: settings.autoescape,
cache: cache,
locals: locals
};
if (settings.varControls) {
defaults.varControls = settings.varControls;
}
swig.setDefaults(defaults);
// swig custom filters
setFilters(swig, settings.filters);
// swig custom tags
setTags(swig, settings.tags);
// add extensions for custom tags
setExtensions(swig, settings.extensions);
function *render(view, options) {
// default extname
var e = extname(view);
if (!e) {
e = '.' + settings.ext;
view += e;
}
// resolve
view = resolve(root, view);
// merge ctx.state
var opts = this.state || {};
// merge ctx.flash, for `koa-flash`
mixin(opts, { flash: this.flash, cache: cache });
// merge settings.locals
mixin(opts, locals);
// merge options
mixin(opts, options || {});
debug('render %s %j', view, opts);
var html = yield renderFile(view, opts);
/* jshint validthis:true */
if (settings.writeBody === true) {
this.body = html;
}
return html;
}
return function*(next) {
this.render = render;
yield* next;
}
}
/**
* Add filters for Swig
*/
function setFilters(swig, filters) {
for (var name in filters) {
swig.setFilter(name, filters[name]);
}
}
/**
* Add tags for Swig
*/
function setTags(swig, tags) {
var name, tag;
for (name in tags) {
tag = tags[name];
swig.setTag(name, tag.parse, tag.compile, tag.ends, tag.blockLevel);
}
}
/**
* Add extensions for Swig
*/
function setExtensions(swig, extensions) {
for (var name in extensions) {
swig.setExtension(name, extensions[name]);
}
}