Created
May 12, 2014 14:12
-
-
Save zzuhan/ee16c23e4dce3181a34a to your computer and use it in GitHub Desktop.
micro tpl 极小的模板系统,支持数据递归
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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