Skip to content

Instantly share code, notes, and snippets.

@greatghoul
Created July 10, 2012 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greatghoul/3080410 to your computer and use it in GitHub Desktop.
Save greatghoul/3080410 to your computer and use it in GitHub Desktop.
Javascript Simple Templating Method
// 格式化字符串
//
// 用法:
//
// var s1 = '%{1} and %{2}!';
// console.log('source: ' + s1);
// console.log('target: ' + fmt(s1, 'ask', 'learn'));
//
// var s2 = "%{name} is %{age} years old, his son's name is %{sons[0].name}";
// console.log('source: ' + s2);
// console.log('target: ' + fmt(s2, { name: 'Lao Ming', age: 32, sons: [{ name: 'Xiao Ming' }]}));
function fmt() {
var args = arguments;
return args[0].replace(/%\{(.*?)}/g, function(match, prop) {
return function(obj, props) {
var prop = /\d+/.test(props[0]) ? parseInt(props[0]) : props[0];
if (props.length > 1) {
return arguments.callee(obj[prop], props.slice(1));
} else {
return (obj[prop] != undefined || obj[prop] != null) ? obj[prop] : '';
}
}(typeof args[1] === 'object' ? args[1] : args, prop.split(/\.|\[|\]\[|\]\./));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment