-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathmicro.js
More file actions
81 lines (73 loc) · 1.9 KB
/
micro.js
File metadata and controls
81 lines (73 loc) · 1.9 KB
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
steal('jquery/view').then(function(){
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
var cache = {};
/**
* @function Micro
* @parent jQuery.View
* @plugin jquery/view/micro
* A very lightweight template engine.
* Magic tags look like:
*
* @codestart
* <h3>{%= message %}</h3>
* @codeend
*
* Micro is integrated in JavaScriptMVC so
* you can use it like:
*
* @codestart
* $("#foo").html('//app/views/bar.micro',{});
* @codeend
*
* ## Pros
*
* - Very Lightweight
*
* ## Cons
*
* - Doesn't handle nested tags.
* - Doesn't handle {%= "%}" %}.
* - More difficult to debug.
* - Removes newlines and tabs.
*
* ## Use
*
* For more information on micro, see John Resig's
* [http://ejohn.org/blog/javascript-micro-templating/ write up].
*
* @param {String} str template content.
* @param {Object} data render's the template with this content.
*/
function Micro(str, data){
var body =
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str.replace(/[\r\t\n]/g, " ")
.replace(/'(?=[^%]*%})/g,"\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(/{%=(.+?)%}/g, "',$1,'")
.split("{%").join("');")
.split("%}").join("p.push('")+ "');}return p.join('');"
var fn = new Function("obj",body);
fn.body = body;
// Provide some basic currying to the user
return data ? fn( data ) : fn;
};
$.View.register({
suffix : "micro",
renderer: function( id, text ) {
var mt = Micro(text)
return function(data){
return mt(data)
}
},
script: function( id, str ) {
return "function(obj){"+Micro(str).body+"}";
}
})
jQuery.View.ext = ".micro"
});