Skip to content

Instantly share code, notes, and snippets.

@JesusLeon
Last active August 29, 2015 14:25
Show Gist options
  • Save JesusLeon/a1d55c126a7524576410 to your computer and use it in GitHub Desktop.
Save JesusLeon/a1d55c126a7524576410 to your computer and use it in GitHub Desktop.
A small JS {string} parser
function parse(template, data) {
var replacer = function (context) {
return function (placeholder, name) {
return context[name];
};
};
return template.replace(/\{(\w+)\}/g, replacer(data));
}
//
// Usage
var template = "Hello, {what}!";
var data = {"what": "World"};
console.log(parse(template, data));
@JesusLeon
Copy link
Author

Found somewhere on the net.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment