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
| function template(str,data) { var strc = str.replace(/\{(\w+)((\.\w+)*)\}/g,function(m,n1,n2) { if(!n2){ if(n1){ if(typeof data == 'string'){ return data; }else{ return; }
}else{ return; } }else{ var tmp = n2.split('.'); tmp.shift(); var result = resultF(tmp,data); if(result&&typeof(result)!=='Object'){ return result; }else{ return ; } }
}); return strc; }
function resultF(arr,obj) { return arr.reduce(function(pre,next) { return pre[next] },obj) }
|