-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
69 lines (64 loc) · 1.76 KB
/
Copy pathrender.js
File metadata and controls
69 lines (64 loc) · 1.76 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
(function($) {
$.fn.render = function($vars) {
/**
* This guy is a theme function to return some markup.
*/
function t(options) {
// Default options.
var defaults = {
el: "p",
html: " ",
attrs: {}
}
// If no option supplied, use default.
for (var i in defaults) {
if (typeof options[i] === 'undefined') {
options[i] = defaults[i];
}
}
// The variables we will use in our markup.
var attrStr = '',
content = options.html,
el = options.el,
html = options.html,
attrs = options.attrs;
// If there are attributes, iterate over them
// and concat them into a single string.
if (typeof attrs == 'object') {
$.each(attrs, function(i, val) {
var attr = ' ' + i + '=\"' + val + '\"';
attrStr += attr;
});
}
/**
* Run the t function on nested html.
* Returns a string of html.
*/
function nestedHTML(html) {
var newHtml = '';
$.each(html, function(i, val) {
newHtml += t(val);
});
return newHtml;
}
// If html is and object then run nestedHTML function on it.
if (typeof html == 'object') {
html = nestedHTML(html);
}
// Different markup for img tags.
if (el === 'img') {
return '<' + el + attrStr + '>';
}
// Return the html element.
return '<' + el + attrStr + '>' + html + '</' + el + '>';
}
// Append the markup to the element.
return this.each(function() {
var $markup = '';
$.each($vars, function(i,v){
$markup += t(v);
});
$(this).append($markup);
});
}
})(jQuery);