-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tpl.js
36 lines (32 loc) · 923 Bytes
/
jquery.tpl.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
/**
* @copyright StagnantIce 2014
* @email [email protected]
* @skype StagnantIce
* @url https://github.com/StagnantIce/tpl.git
*/
$.fn.outerHtml = function(s) {
var $obj = $(this).clone();
var s = s || $obj.html();
return $('<div>').append($obj.html(s)).html();
}
$.fn.tpl = function(params) {
var $scope = $(this).clone();
$.each(params, function(index, value){
if (value === false) {
$(index, $scope).empty();
} else if ($.isArray(value)) {
var a = [];
$.each(value, function(i,v) {
a.push( $(index, $scope).outerHtml($(index, $scope).tpl(v) ) );
});
$(index, $scope).replaceWith(a.join(''));
} else if ($.isFunction(value)) {
$.proxy(value, $(index, $scope))();
} else if ($.isPlainObject(value)) {
$(index, $scope).replaceWith( $(index, $scope).outerHtml($(index, $scope).tpl(value) ) );
} else {
$(index, $scope).html(value);
}
});
return $scope.html();
}