This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.js
159 lines (139 loc) · 4.78 KB
/
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
// TradingView task
// Kuvakin Sergey (https://github.com/lafin)
var templateEngine = (function () {
// private
function renderBlock(template, data) {
var endBlockPattern = '{% / %}';
var start = template.search(/%\}/gi),
end = template.lastIndexOf(endBlockPattern);
var head = template.substring(0, template.search(/\{%/gi)),
footer = template.substring(end + endBlockPattern.length, template.length);
template = template.substring(start + 2, end);
if (!~template.indexOf(endBlockPattern)) {
for (var i in data) {
if (data.hasOwnProperty(i)) {
head += template.replace(/\{(%|\{){1}\s*\.\s*(%|\}){1}\}/gi, data[i]);
}
}
} else {
for (var j in data) {
if (data.hasOwnProperty(j)) {
head += renderBlock(template, data[j]);
}
}
}
return head + footer;
}
function applyFilter(filters, str) {
for (var i in filters) {
if (filters.hasOwnProperty(i)) {
var name = filters[i].substring(1);
if (templateEngine.hasOwnProperty(name)) {
str = templateEngine[name](str);
}
}
}
return str;
}
function parseTemplate(template, data) {
var start = 0,
end = 0,
num = 0,
offset = 0,
name, html = '';
template = template.replace(/\{\{\s*(\w+)(:.*?)*\s*\}\}/gi, function (str, subStr) { // simple block
var defaultValue = /default\((.*?)\)/.exec(str);
var returnValue = data.hasOwnProperty(subStr) ? data[subStr] : ( !! defaultValue ? defaultValue[1] : '');
var filters = str.match(/:\w+/gi);
return !!filters ? applyFilter(filters, returnValue) : returnValue;
});
var match = template.match(/\{%\s*(\w+|\/|\.)\s*%\}/gi);
for (var i in match) {
if (match.hasOwnProperty(i)) {
var pattern = /\{%\s*(.*?)\s*%\}/.exec(match[i])[1];
if (pattern === '/') {
num -= 1;
if (num === 0) {
end = template.indexOf(match[i], offset) + match[i].length;
html += renderBlock(template.substring(start, end), data[name]);
} else {
offset = template.indexOf(match[i], offset) + match[i].length;
}
} else {
if (num === 0) {
offset = start = template.indexOf(match[i]);
name = pattern;
html += template.substring(end, start);
}
num += 1;
}
}
}
return html + template.substring(end);
}
// public
return {
template: null,
data: null,
upper: function (str) {
return str.toUpperCase();
},
lower: function (str) {
return str.toLowerCase();
},
escape: function (str) {
return str.replace('&', '&').replace('<', '<').replace('>', '>');
},
trim: function (str) {
return str.trim();
},
capitalize: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
setTemplate: function (_template) {
this.template = _template;
return this;
},
setData: function (_data) {
this.data = _data;
return this;
},
render: function (template, data) {
template = template || this.template;
data = data || this.data;
template = template.replace(/\{#(.*?)#\}/gi, ''); // comment block
template = template.replace(/\n/gi, '###n');
template = parseTemplate(template, data);
return template.replace(/###n/gi, '\n');
}
};
}());
templateEngine.myfilter = function (str) {
return '*** ' + str + ' ***';
};
var tpl = '<h1>Category: {{category}}</h1>\n' +
'<h1>Category: {{category:lower:escape:trim:capitalize:upper}}</h1>\n' +
'<h1>My filter: {{ddd:default(lololo):myfilter}}</h1>\n' +
'<ol>\n' +
'{# items must be non-empty for valid markup #}' +
'{% items %}' +
' <li>{{ . }}</li>\n' +
'{% / %}' +
'</ol>\n' +
'<table>\n' +
'{% table %}' +
' <tr>\n' +
' {% . %}' +
'<td>{{ . }}</td>' +
'{% / %}' +
'\n </tr>\n' +
'{% / %}' +
'</table>\n';
console.log(templateEngine.render(tpl, {
category: 'Fruits',
items: ['Mango', 'Banana', 'Orange'],
table: [
[1, 2, 3],
[4, 5, 6]
]
}));