/
index.js
114 lines (94 loc) · 3 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
var _ = require('lodash'),
jqtpl = require('jqtpl'),
util = require('util'),
events = require('events'),
tmpl = jqtpl.express;
var Jqtpl = function() { };
util.inherits(Jqtpl, events.EventEmitter);
Jqtpl.prototype.attach = function (options) {
var config = options.config;
this.render = function (res, viewName, data, headers) {
var err = null,
html = '',
layouthtml = null,
layoutViewName = null,
// add express specific compatibility overrides - partial and layout
expressExtensions = {
partial: function(viewName, localdata) {
var d = localdata || data;
if (localdata) {
d = _.extend(_.clone(data), localdata);
}
return renderHtml(viewName, d, expressExtensions, config);
},
layout: function(viewName) {
layoutViewName = viewName;
}
};
// render the main view.
try {
html = renderHtml(viewName, data, expressExtensions, config);
}
catch (e) {
err = e;
}
try {
// if layout was found during initial render, then it was assigned to the layout property. We need to process the layout now.
if (layoutViewName) {
data.body = html;
layouthtml = renderHtml(layoutViewName, data, expressExtensions, config);
}
}
catch (e) {
e.message += "Error in layout template";
err = e;
}
// write the response
if (err) {
res.writeHead(500, _.extend({ 'Content-Type': 'text/plain' }, headers) );
res.write(err.message + '\n' + err.stack);
res.end();
}
else {
res.writeHead(200, _.extend({ 'Content-Type': 'text/html' }, headers) );
res.write(layouthtml || html);
res.end();
}
};
};
// `exports.init` gets called by broadway on `app.init`.
Jqtpl.prototype.init = function (done) {
// This plugin doesn't require any initialization step.
return done();
};
var renderHtml = function(viewName, data, expressExtensions, config){
var html = '',
view = {
path: config.getView(viewName),
contents: config.getViewMarkup(viewName)
};
if (!view.path) {
var html = 'jqtpl plugin: Cannot find view - ' + viewName;
}
else {
// in debug, compile template every time. WILL NOT SCALE IN PRODUCTION
if (config.html.debug) {
delete jqtpl.template[view.path];
jqtpl.template(view.path, view.contents);
}
else {
config.templateCache[view.path] = ( config.templateCache[view.path] || jqtpl.template(view.path, view.contents) ); // precompile the template and cache it using filename // tmpl.compile(markup, { filename: view, scope: jqtpl });
}
_.extend(data, { config: config, utils: config.utils }); // insure utils are always in the data
_.extend(data, expressExtensions); // insure express exts for jqtpl are there too.
try {
html = jqtpl.tmpl(view.path, data); // render
}
catch (e) {
e.message = 'ERROR in template: ' + viewName + ' (' + view.path + '). ' + e.message;
throw e;
}
}
return html;
};
module.exports = Jqtpl;