-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathtwig.exports.js
239 lines (201 loc) · 7.02 KB
/
twig.exports.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// ## twig.exports.js
//
// This file provides extension points and other hooks into the twig functionality.
module.exports = function (Twig) {
'use strict';
Twig.exports = {
VERSION: Twig.VERSION
};
/**
* Create and compile a twig.js template.
*
* @param {Object} param Paramteres for creating a Twig template.
*
* @return {Twig.Template} A Twig template ready for rendering.
*/
Twig.exports.twig = function (params) {
'use strict';
const {id} = params;
const options = {
strictVariables: params.strict_variables || false,
// TODO: turn autoscape on in the next major version
autoescape: (params.autoescape !== null && params.autoescape) || false,
allowInlineIncludes: params.allowInlineIncludes || false,
rethrow: params.rethrow || false,
namespaces: params.namespaces
};
if (Twig.cache && id) {
Twig.validateId(id);
}
if (params.debug !== undefined) {
Twig.debug = params.debug;
}
if (params.trace !== undefined) {
Twig.trace = params.trace;
}
if (params.data !== undefined) {
return Twig.Templates.parsers.twig({
data: params.data,
path: Object.hasOwnProperty.call(params, 'path') ? params.path : undefined,
module: params.module,
id,
options
});
}
if (params.ref !== undefined) {
if (params.id !== undefined) {
throw new Twig.Error('Both ref and id cannot be set on a twig.js template.');
}
return Twig.Templates.load(params.ref);
}
if (params.method !== undefined) {
if (!Twig.Templates.isRegisteredLoader(params.method)) {
throw new Twig.Error('Loader for "' + params.method + '" is not defined.');
}
return Twig.Templates.loadRemote(params.name || params.href || params.path || id || undefined, {
id,
method: params.method,
parser: params.parser || 'twig',
base: params.base,
module: params.module,
precompiled: params.precompiled,
async: params.async,
options
}, params.load, params.error);
}
if (params.href !== undefined) {
return Twig.Templates.loadRemote(params.href, {
id,
method: 'ajax',
parser: params.parser || 'twig',
base: params.base,
module: params.module,
precompiled: params.precompiled,
async: params.async,
options
}, params.load, params.error);
}
if (params.path !== undefined) {
return Twig.Templates.loadRemote(params.path, {
id,
method: 'fs',
parser: params.parser || 'twig',
base: params.base,
module: params.module,
precompiled: params.precompiled,
async: params.async,
options
}, params.load, params.error);
}
};
// Extend Twig with a new filter.
Twig.exports.extendFilter = function (filter, definition) {
Twig.filter.extend(filter, definition);
};
// Extend Twig with a new function.
Twig.exports.extendFunction = function (fn, definition) {
Twig._function.extend(fn, definition);
};
// Extend Twig with a new test.
Twig.exports.extendTest = function (test, definition) {
Twig.test.extend(test, definition);
};
// Extend Twig with a new definition.
Twig.exports.extendTag = function (definition) {
Twig.logic.extend(definition);
};
// Provide an environment for extending Twig core.
// Calls fn with the internal Twig object.
Twig.exports.extend = function (fn) {
fn(Twig);
};
/**
* Provide an extension for use with express 2.
*
* @param {string} markup The template markup.
* @param {array} options The express options.
*
* @return {string} The rendered template.
*/
Twig.exports.compile = function (markup, options) {
const id = options.filename;
const path = options.filename;
// Try to load the template from the cache
const template = new Twig.Template({
data: markup,
path,
id,
options: options.settings['twig options']
}); // Twig.Templates.load(id) ||
return function (context) {
return template.render(context);
};
};
/**
* Provide an extension for use with express 3.
*
* @param {string} path The location of the template file on disk.
* @param {Object|Function} The options or callback.
* @param {Function} fn callback.
*
* @throws Twig.Error
*/
Twig.exports.renderFile = function (path, options, fn) {
// Handle callback in options
if (typeof options === 'function') {
fn = options;
options = {};
}
options = options || {};
const settings = options.settings || {};
// Mixin any options provided to the express app.
const viewOptions = settings['twig options'];
const params = {
path,
base: settings.views,
load(template) {
// Render and return template as a simple string, see https://github.com/twigjs/twig.js/pull/348 for more information
if (!viewOptions || !viewOptions.allowAsync) {
fn(null, String(template.render(options)));
return;
}
template.renderAsync(options)
.then(out => fn(null, out), fn);
},
error(err) {
fn(err);
}
};
if (viewOptions) {
for (const option in viewOptions) {
if (Object.hasOwnProperty.call(viewOptions, option)) {
params[option] = viewOptions[option];
}
}
}
Twig.exports.twig(params);
};
// Express 3 handler
Twig.exports.__express = Twig.exports.renderFile;
/**
* Shoud Twig.js cache templates.
* Disable during development to see changes to templates without
* reloading, and disable in production to improve performance.
*
* @param {boolean} cache
*/
Twig.exports.cache = function (cache) {
Twig.cache = cache;
};
// We need to export the path module so we can effectively test it
Twig.exports.path = Twig.path;
// Export our filters.
// Resolves #307
Twig.exports.filters = Twig.filters;
// Export our tests.
Twig.exports.tests = Twig.tests;
// Export our functions.
Twig.exports.functions = Twig.functions;
Twig.exports.Promise = Twig.Promise;
return Twig;
};