-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.templates.js
50 lines (45 loc) · 1.5 KB
/
jquery.templates.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
(function($) {
$.Template = function(str, options){
this.settings = $.extend({}, $.Template.defaults, options);
this.str = $.trim($('<div>').append($(str).clone()).remove().html()); // modify to take function
this.fill = function(params) {
if (this.settings.beforeFill.apply(this) === false) {
return '';
}
regex = new RegExp(this.settings.begin_sep + "(.+?)" + this.settings.end_sep, "g");
var filled = this.str.replace(regex,
function (m, key) {
var value = params[key];
return typeof value === 'string' || typeof value === 'number' ? value : "";
});
this.settings.afterFill.apply(this);
return filled;
};
};
$.Template.defaults = {
"begin_sep" : "__",
"end_sep": "__",
"beforeFill": function(){},
"afterFill": function(){}
};
(function(oldDomManip){
$.fn.domManip = function() {
var args = arguments[0];
if(args && args.length > 1 && args[0] instanceof $.Template) {
arguments[0] = [ args[0].fill(args[1]) ];
}
// Call the original method
return oldDomManip.apply(this, arguments);
};
})($.fn.domManip);
$.fn.templatize = function(params, options){
var $templates = $([]);
this.each(function(index, element){
var template = new $.Template($(element), options);
var p = $.isArray(params)? params[index]||{} : params;
var t = template.fill(p);
$templates = $templates.add(t);
});
return $templates;
};
})(jQuery);