-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.template.js
255 lines (223 loc) · 7 KB
/
jquery.template.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* jQuery Templates
*
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Written by: Stan Lemon <[email protected]>
*
* Based off of the Ext.Template library, available at:
* http://www.extjs.com
*
* This library provides basic templating functionality, allowing for macro-based
* templates within jQuery.
*
* Basic Usage:
*
* var t = $.template('<div id="foo">Hello ${name}, how are you ${question}? I am ${me:substr(0,10)}</div>');
*
* $(selector).append( t , {
* name: 'Stan',
* question: 'feeling',
* me: 'doing quite well myself, thank you very much!'
* });
*
* Requires: jQuery 1.2+
*
*
* @todo Add callbacks to the DOM manipulation methods, so that events can be bound
* to template nodes after creation.
*/
(function($){
/**
* Create a New Template
*/
$.template = function(html, options) {
return new $.template.instance(html, options);
};
/**
* Template constructor - Creates a new template instance.
*
* @param html The string of HTML to be used for the template.
* @param options An object of configurable options. Currently
* you can toggle compile as a boolean value and set a custom
* template regular expression on the property regx by
* specifying the key of the regx to use from the regx object.
*/
$.template.instance = function(html, options) {
// If a custom regular expression has been set, grab it from the regx object
if ( options && options['regx'] ) options.regx = this.regx[ options.regx ];
this.options = $.extend({
compile: false,
regx: this.regx.standard
}, options || {});
this.html = html;
if (this.options.compile) {
this.compile();
}
this.isTemplate = true;
};
/**
* Regular Expression for Finding Variables
*
* The default pattern looks for variables in JSP style, the form of: ${variable}
* There are also regular expressions available for ext-style variables and
* jTemplate style variables.
*
* You can add your own regular expressions for variable ussage by doing.
* $.extend({ $.template.re , {
* myvartype: /...../g
* }
*
* Then when creating a template do:
* var t = $.template("<div>...</div>", { regx: 'myvartype' });
*/
$.template.regx = $.template.instance.prototype.regx = {
jsp: /\$\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
ext: /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
jtemplates: /\{\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}\}/g
};
/**
* Set the standard regular expression to be used.
*/
$.template.regx.standard = $.template.regx.jsp;
/**
* Variable Helper Methods
*
* This is a collection of methods which can be used within the variable syntax, ie:
* ${variable:substr(0,30)} Which would only print a substring, 30 characters in length
* begining at the first character for the variable named "variable".
*
* A basic substring helper is provided as an example of how you can define helpers.
* To add more helpers simply do:
* $.extend( $.template.helpers , {
* sampleHelper: function() { ... }
* });
*/
$.template.helpers = $.template.instance.prototype.helpers = {
substr : function(value, start, length){
return String(value).substr(start, length);
}
};
/**
* Template Instance Methods
*/
$.extend( $.template.instance.prototype, {
/**
* Apply Values to a Template
*
* This is the macro-work horse of the library, it receives an object
* and the properties of that objects are assigned to the template, where
* the variables in the template represent keys within the object itself.
*
* @param values An object of properties mapped to template variables
*/
apply: function(values) {
if (this.options.compile) {
return this.compiled(values);
} else {
var tpl = this;
var fm = this.helpers;
var fn = function(m, name, format, args) {
if (format) {
if (format.substr(0, 5) == "this."){
return tpl.call(format.substr(5), values[name], values);
} else {
if (args) {
// quoted values are required for strings in compiled templates,
// but for non compiled we need to strip them
// quoted reversed for jsmin
var re = /^\s*['"](.*)["']\s*$/;
args = args.split(',');
for(var i = 0, len = args.length; i < len; i++) {
args[i] = args[i].replace(re, "$1");
}
args = [values[name]].concat(args);
} else {
args = [values[name]];
}
return fm[format].apply(fm, args);
}
} else {
return values[name] !== undefined ? values[name] : "";
}
};
return this.html.replace(this.options.regx, fn);
}
},
/**
* Compile a template for speedier usage
*/
compile: function() {
var sep = $.browser.mozilla ? "+" : ",";
var fm = this.helpers;
var fn = function(m, name, format, args){
if (format) {
args = args ? ',' + args : "";
if (format.substr(0, 5) != "this.") {
format = "fm." + format + '(';
} else {
format = 'this.call("'+ format.substr(5) + '", ';
args = ", values";
}
} else {
args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
}
return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
};
var body;
if ($.browser.mozilla) {
body = "this.compiled = function(values){ return '" +
this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.options.regx, fn) +
"';};";
} else {
body = ["this.compiled = function(values){ return ['"];
body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.options.regx, fn));
body.push("'].join('');};");
body = body.join('');
}
eval(body);
return this;
}
});
/**
* Save a reference in this local scope to the original methods which we're
* going to overload.
**/
var $_old = {
domManip: $.fn.domManip,
text: $.fn.text,
html: $.fn.html
};
/**
* Overwrite the domManip method so that we can use things like append() by passing a
* template object and macro parameters.
*/
$.fn.domManip = function( args, table, reverse, callback ) {
if (args[0].isTemplate) {
// Apply the template and it's arguments...
args[0] = args[0].apply( args[1] );
// Get rid of the arguements, we don't want to pass them on
delete args[1];
}
// Call the original method
var r = $_old.domManip.apply(this, arguments);
return r;
};
/**
* Overwrite the html() method
*/
$.fn.html = function( value , o ) {
if (value && value.isTemplate) var value = value.apply( o );
var r = $_old.html.apply(this, [value]);
return r;
};
/**
* Overwrite the text() method
*/
$.fn.text = function( value , o ) {
if (value && value.isTemplate) var value = value.apply( o );
var r = $_old.text.apply(this, [value]);
return r;
};
})(jQuery);