-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.qtmpl.js
49 lines (43 loc) · 1.23 KB
/
jquery.qtmpl.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
/*
* jquery.qtmpl.js
*
* Created by Michael C. Beck on 2010-01-18.
* Copyright qualeapps.com. All rights reserved. See license for details
* more info at http://github.com/awesomecode/qtmpl
*
*/
(function($) {
$.fn.qTmpl = function(tmplId, data) {
var trimmed = $.trim($(tmplId).html());
return this.each(function(){
var el = $(this);
var bla = '';
if($.isArray(data)){
$.each(data, function(key, val) {
bla += trimmed.replace(/{{=(.+?)}}/g, function(matched, id) { return checkExistance(id, val)})
})
} else {
bla += trimmed.replace(/{{=(.+?)}}/g, function(matched, id) { return checkExistance(id, data)})
}
el.html(bla);
});
};
// Check for existance of value or return an empty string
function checkExistance(prop, obj) {
var parts = prop.split("."),
value = '';
for (var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
if (obj !== null && typeof obj === "object" && part in obj) {
obj = obj[part];
if (i == l-1) {
value = obj;
}
}
else {
value = "";
}
}
return value;
};
})(jQuery);