-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude.js
More file actions
67 lines (53 loc) · 1.7 KB
/
Copy pathinclude.js
File metadata and controls
67 lines (53 loc) · 1.7 KB
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
var helpers = require('../helpers'),
_ = require('underscore');
/**
* include
*/
module.exports = function (indent, parentBlock, parser) {
var args = _.clone(this.args),
template = args.shift(),
context = '_context',
ignore = false,
out = '',
ctx;
indent = indent || '';
if (!helpers.isLiteral(template) && !helpers.isValidName(template)) {
throw new Error('Invalid arguments passed to \'include\' tag.');
}
if (args.length) {
if (_.last(args) === 'only') {
context = '{}';
args.pop();
}
if (args.length > 1 && args[0] === 'ignore' & args[1] === 'missing') {
args.shift();
args.shift();
ignore = true;
}
if (args.length && args[0] !== 'with') {
throw new Error('Invalid arguments passed to \'include\' tag.');
}
if (args[0] === 'with') {
args.shift();
if (!args.length) {
throw new Error('Context for \'include\' tag not provided, but expected after \'with\' token.');
}
ctx = args.shift();
context = '_context["' + ctx + '"] || ' + ctx;
}
}
out = '(function () {\n' +
helpers.setVar('__template', parser.parseVariable(template)) + '\n' +
' var includeContext = ' + context + ';\n';
if (ignore) {
out += 'try {\n';
}
out += ' if (typeof __template === "string") {\n';
out += ' _output += _this.compileFile(__template).render(includeContext, _parents);\n';
out += ' }\n';
if (ignore) {
out += '} catch (e) {}\n';
}
out += '})();\n';
return out;
};