Skip to content

Instantly share code, notes, and snippets.

@chenminhua
Created May 1, 2017 07:10
Show Gist options
  • Save chenminhua/7434e57604f2114d4cf205b41be129c8 to your computer and use it in GitHub Desktop.
Save chenminhua/7434e57604f2114d4cf205b41be129c8 to your computer and use it in GitHub Desktop.
模板引擎
String.prototype.render = function(obj) {
return this.replace(/\$\{(\w+|\w+\.\w+)\}/g, match => {
var keys = match.replace('${', '').replace('}', '').split('.')
return keys.reduce((acc, cv) => acc[cv], obj)
})
}
String.prototype.render = function (obj) {
with(obj) {
return eval('`' + this + '`')
}
}
String.prototype.render = function(employee) {
var str = this + '';
return str.replace(/\$\{(\w+|\w+\.\w+)\}/g, function(match) {
var key = match.replace('${', '').replace('}', '');
if (/\./g.test(key)) {
var key = key.split('.');
var value = employee;
for( var i =0; i<key.length; i++ ){
value = value[key[i]]
}
return value;
} else {
return employee[key]
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment