Skip to content

Instantly share code, notes, and snippets.

@ManasJayanth
Last active August 29, 2015 14:03
Show Gist options
  • Save ManasJayanth/be5dbe43992933b337ac to your computer and use it in GitHub Desktop.
Save ManasJayanth/be5dbe43992933b337ac to your computer and use it in GitHub Desktop.
Javascript alternative for ruby, python's formatted string
/**
* Formatted string in javascript
*/
function renderTemplate (template, obj) {
return template.replace(/\{\{[a-zA-Z]+\}\}/g, function (m) {
for (var key in obj) {
if (m.indexOf(key) !== -1) {
return obj[key];
}
}
});
}
console.log(renderTemplate("{{a}} {{b}}", {a: 'hello', b: 'world'}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment