/
templating.js
119 lines (109 loc) · 3.25 KB
/
templating.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
var fs = require("fs");
require('josi/class');
var TemplatingEngine = Class.extend({
init: function() {
var cache = {};
var self = this;
this.load = function(path, cb) {
if (cache[path]) {
cb(cache[path]);
} else {
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
// todo: deal with error when loading view
throw err;
}
cache[path] = self.compile(data);
cb(cache[path]);
});
}
};
},
compile: function(str) {
throw new Error('Not implemented error. To use create a subclass of TemplatingEngine that implements compile.');
},
render: function(data, callback, view, master) {
var self = this;
this.load(view, function(template) {
var rendered = template(data);
if (master) {
// fixme: don't clobber users' data
data.main = rendered;
self.load(master, function(masterTemplate) {
var rendered = masterTemplate(data);
callback(rendered);
});
} else {
callback(rendered);
}
});
}
});
// JavaScript Micro-Templating
// by John Resig - http://ejohn.org/ - MIT Licensed
// http://ejohn.org/blog/javascript-micro-templating/
this.MicroTemplatingEngine = TemplatingEngine.extend({
compile: function(str) {
return new Function("obj",
"var __=[];" +
// Introduce the data as local variables using with(){}
"with(obj){__.push('" +
// Convert the template into pure JavaScript
str
.replace(/[\r\n\t]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("__.push('")
.split("\r").join("\\'")
+ "');}return __.join('');"
);
}
});
// Adapted from jQuery Templating Plugin
// Copyright 2010, John Resig
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://github.com/jquery/jquery-tmpl/
this.jQueryTemplatingEngine = TemplatingEngine.extend({
compile: function(str) {
var tmplcmd = {
'each': {
_default: [null, "$i"],
prefix: "for(var $2 in $1){",
suffix: "}"
},
'if': {
prefix: "if($1){",
suffix: "}"
},
'else': {
prefix: "}else{"
},
'=': {
_default: ["this"],
prefix: "_.push(typeof $1==='function'?$1.call(this):$1);"
}
};
var fn = new Function("$data",
"var _=[];_.data=$data;" +
// Introduce the data as local variables using with(){}
"with($data){_.push('" +
// Convert the template into pure JavaScript
str
.replace(/[\r\t\n]/g, " ")
.replace(/\${([^}]*)}/g, "{{= $1}}")
.replace(/{{(\/?)(\w+|.)(?:\((.*?)\))?(?: (.*?))?}}/g, function(all, slash, type, fnargs, args) {
var tmpl = tmplcmd[type];
if (!tmpl) {
throw "Template command not found: " + type;
}
var def = tmpl._default;
return "');" + tmpl[slash ? "suffix" : "prefix"]
.split("$1").join(args || (def ? def[0] : ''))
.split("$2").join(fnargs || (def ? def[1] : '')) + "_.push('";
})
+ "');}return _.join('');");
return fn;
}
});