forked from 1602/ejs-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (67 loc) · 2.36 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
var fs = require('fs');
// monkey patch ejs
try {
var ejs = require('ejs'), old_parse = ejs.parse;
ejs.parse = function () {
var str = old_parse.apply(this, Array.prototype.slice.call(arguments));
return str.replace('var buf = [];', 'var buf = []; arguments.callee.buf = buf;');
};
} catch (e) {
// disregard if ejs is not installed yet
}
/**
* This extension will be used by default for all template files
*/
exports.extension = '.ejs';
/**
* Original templating engine
*/
exports.module = 'ejs';
/**
* Get source template filename
*/
exports.template = function (name) {
return __dirname + '/templates/' + name + '.ejs';
};
exports.templateText = function (name, data) {
switch (name) {
case 'default_action_view':
return '<div class="page-header"><h1>' + data.join('#') + '</h1></div>\n';
case 'scaffold_show':
var fields = [];
data.forEach(function (property) {
switch (property.type) {
default:
fields.push('<tr><th>' + property.name + '</th><td><%= model.' + property.name + ' %></td></tr>');
break;
}
});
return fs.readFileSync(exports.template('scaffold_show')).toString().replace('FIELDS', fields.join('\n '));
case 'scaffold_form':
var form = '<%- errorMessagesFor(model) %>\n';
data.forEach(function (property) {
switch (property.type) {
case 'Boolean':
form += [
'<div class="control-group">',
' <%- form.label("' + property.name + '", false, {class: "control-label"}) %>',
' <div class="controls">',
' <%- form.checkbox("' + property.name + '") %>',
' </div>',
'</div>'
].join('\n') + '\n';
break;
default:
form += [
'<div class="control-group">',
' <%- form.label("' + property.name + '", false, {class: "control-label"}) %>',
' <div class="controls">',
' <%- form.input("' + property.name + '") %>',
' </div>',
'</div>'
].join('\n') + '\n';
}
});
return form;
}
};