clean up template interpolation code #1969
This commit is contained in:
parent
1581a94fc3
commit
d0f768ba36
@ -106,31 +106,18 @@ L.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
compileTemplate: function (str, data) {
|
compileTemplate: function (str, data) {
|
||||||
/*jslint evil: true */
|
// based on https://gist.github.com/padolsey/6008842
|
||||||
//from https://gist.github.com/padolsey/6008842
|
str = str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
|
||||||
return new Function(
|
return '" + o["' + key + '"]' + (typeof data[key] === 'function' ? '(o)' : '') + ' + "';
|
||||||
'o',
|
});
|
||||||
'return "' + (
|
// jshint evil: true
|
||||||
str.replace(/\"/g, '\\"').replace(/\{ *([\w_]+) *\}/g, function (_, $1) {
|
return new Function('o', 'return "' + str + '";');
|
||||||
if (typeof data[$1] === 'function') {
|
|
||||||
return '" + o["' + $1 + '"](o) + "';
|
|
||||||
} else {
|
|
||||||
return '" + o["' + $1 + '"] + "';
|
|
||||||
}
|
|
||||||
})
|
|
||||||
) + '";'
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
templateCache: {},
|
|
||||||
|
|
||||||
template: function (str, data) {
|
template: function (str, data) {
|
||||||
if (str in this.templateCache) {
|
var cache = L.Util._templateCache = L.Util._templateCache || {};
|
||||||
return this.templateCache[str](data);
|
cache[str] = cache[str] || L.Util.compileTemplate(str, data);
|
||||||
} else {
|
return cache[str](data);
|
||||||
this.templateCache[str] = this.compileTemplate(str, data);
|
|
||||||
return this.templateCache[str](data);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
isArray: function (obj) {
|
isArray: function (obj) {
|
||||||
|
Loading…
Reference in New Issue
Block a user