/
partial.js
168 lines (143 loc) · 4.54 KB
/
partial.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
var Partial
, Layout
, adapter = require('./adapters')
, path = require('path')
, fs = require('fs')
, utils = require('utilities')
, cache = {};
Partial = function (templatePath, data, parent) {
var self = this;
this.id = utils.string.uuid();
this.templatePath = templatePath
this.data = data || {};
this.parent = parent;
this.children = [];
this.content = '';
// Hang a `partial` method on the execution-context for the
// template rendering (e.g., will be the EJS global `partial`
// function to add sub-templates
this.data.partial = function (templatePath, data) {
var child = new Partial(templatePath, data, self);
self.addChild(child);
return '###partial###' + child.id
};
};
Partial.prototype = new (function () {
this.getTemplateData = function () {
var dirs = []
, dir
, key
, templatePath = path.normalize(this.templatePath)
, templateData;
// Look for an exact match
templateData = geddy.templateRegistry[templatePath];
// No exact match, try with some directory prefixes
if (!templateData) {
// Loop through dirs until a registered template path is found
// Note: Template paths are gathered at init so we don't have to
// touch the FS when looking for templates
// If this is a child partial, then look in the parent's directory
if (this.parent) {
dirs.unshift(path.dirname(this.parent.templatePath));
}
// Any local template directory
dirs.unshift(path.dirname(templatePath));
// Last resort; look in the base views directory
dirs.unshift(path.normalize('app/views'));
for (var i = 0, ii = dirs.length; i < ii; i++) {
dir = dirs[i];
// Not full path (No extension(s))
key = path.normalize(path.join(dir, templatePath));
templateData = geddy.templateRegistry[key];
if (templateData) {
break;
}
}
}
// Still no joy
if (!templateData) {
// Is this a Layout?
if (this instanceof Layout) {
// Try to use the default application layout
key = path.normalize('app/views/layouts/application');
templateData = geddy.templateRegistry[key];
// If they've removed the default layout for some reason
if (!templateData) {
throw new Error('Layout template "' + templatePath + '" not found in ' +
utils.array.humanize(dirs));
}
}
// If it's a normal Partial then it doesn't exist, boom
else {
throw new Error('Partial template "' + templatePath + '" not found in ' +
utils.array.humanize(dirs));
}
}
return templateData || null;
};
this.render = function (cb) {
var self = this
, templateData = this.getTemplateData()
, file = templateData.file
, ext = templateData.ext
, handleData = function (data) {
self.renderSelf(data, ext);
self.renderChildren(cb);
};
// Use cached template content if possible
if (cache[file]) {
handleData(cache[file]);
}
// Otherwise fetch off disk
else {
// Get the template from the FS then cache it for subsequent requests
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
throw err;
}
if (geddy.config.environment != 'development') {
cache[file] = data;
}
handleData(data);
});
}
};
this.renderSelf = function (templateContent, ext) {
var ctor = adapter.getCtorForExt(ext)
, templateAdapter = new ctor(templateContent);
/*
var adapter = new Adapter();
// Render with appropriate engine
adapter.set({engine: ext, template: templateContent});
*/
this.content = templateAdapter.render(this.data);
};
this.renderChildren = function (cb) {
var self = this
, children = this.children
, incr = children.length;
if (children.length) {
children.forEach(function (child) {
child.render(function (content) {
// Must replace $ with $$ otherwise capture groups screw up
self.content = self.content.replace(
'###partial###' + child.id, content.replace('$','$$$$'));
incr--;
if (!incr) {
cb(self.content);
}
});
});
}
else {
cb(this.content);
}
};
this.addChild = function (child) {
this.children.push(child);
};
})();
exports.Partial = Partial;
// Layout is a subclass of Partial, depends on it
// being defined
Layout = require('./layout').Layout;