Skip to content

Instantly share code, notes, and snippets.

@zzuhan
Created May 12, 2014 14:12
Show Gist options
  • Save zzuhan/ee16c23e4dce3181a34a to your computer and use it in GitHub Desktop.
Save zzuhan/ee16c23e4dce3181a34a to your computer and use it in GitHub Desktop.
micro tpl 极小的模板系统,支持数据递归
var microTpl = {
render: function (tpl, data) {
if(!tpl || !data) {
throw new Error("tpl or data can't be empty, microlTpl.render");
}
// 占位符的正则 the regex of placeholder
var rendered,
rPlaceholder = /\{([^}]+)\}/g,
rendered = tpl.replace(rPlaceholder, function (m, key) {
var result,
stack = key.split('.');
result = data;
while(key = stack.shift()){
result = result[key];
}
return result;
});
return rendered;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment