This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ejs-engine.js
144 lines (139 loc) · 4.84 KB
/
ejs-engine.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
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-06-09
*/
/**
* @ignore
*/
var app = require('./index'),
async = require('async'),
ejs = require('ejs'),
util = require('util'),
path = require('path');
/**
* @class EjsEngine
* @param {HttpContext=} context
* @constructor
* @property {HttpContext} context Gets or sets an instance of HttpContext that represents the current HTTP context.
*/
function EjsEngine(context) {
/**
* @type {HttpContext}
*/
var ctx = context;
Object.defineProperty(this,'context', {
get: function() {
return ctx;
},
set: function(value) {
ctx = value;
},
configurable:false,
enumerable:false
});
}
/**
* Adds a EJS filter to filters collection.
* @param {string} name
* @param {Function} fn
*/
EjsEngine.prototype.filter = function(name, fn) {
ejs.filters[name] = fn;
};
/**
*
* @param {string} filename
* @param {*=} data
* @param {Function} callback
*/
EjsEngine.prototype.render = function(filename, data, callback) {
var self = this;
try {
var fs = require('fs'), common = require('./common');
fs.readFile(filename,'utf-8', function(err, str) {
try {
if (err) {
if (err.code === 'ENOENT') {
//throw not found exception
return callback(new common.HttpNotFoundException('View layout cannot be found.'));
}
return callback(err);
}
else {
//get view header (if any)
var matcher = /^(\s*)<%#(.*?)%>/;
var properties = { layout:null };
if (matcher.test(str)) {
var matches = matcher.exec(str);
properties = JSON.parse(matches[2]);
//remove match
str = str.replace(matcher,'');
}
//create view context
var viewContext = app.views.createViewContext(self.context);
//extend view context with page properties
util._extend(viewContext, properties || {});
//set view context data
viewContext.data = data;
var partial = false;
if (self.context && self.context.request.route)
partial = common.parseBoolean(self.context.request.route['partial']);
if (properties.layout && !partial) {
var layout;
if (/^\//.test(properties.layout)) {
//relative to application folder e.g. /views/shared/master.html.ejs
layout = app.current.mapPath(properties.layout);
}
else {
//relative to view file path e.g. ./../master.html.html.ejs
layout = path.resolve(filename, properties.layout);
}
//set current view buffer (after rendering)
viewContext.body = ejs.render(str, viewContext);
//render master layout
fs.readFile(layout,'utf-8', function(err, layoutData) {
try {
if (err) {
if (err.code === 'ENOENT') {
return callback(new common.HttpNotFoundException('Master view layout cannot be found'));
}
return callback(err);
}
var result = ejs.render(layoutData, viewContext);
callback(null, result);
}
catch (e) {
callback(e);
}
});
}
else {
var result = ejs.render(str, viewContext);
callback(null, result);
}
}
}
catch (e) {
callback(e);
}
});
}
catch (e) {
callback.call(self, e);
}
};
/**
*
* @param {HttpContext=} context
* @returns {EjsEngine}
*/
EjsEngine.prototype.createInstance = function(context) {
return new EjsEngine(context);
};
if (typeof exports !== 'undefined') module.exports.createInstance = EjsEngine.prototype.createInstance;