Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active February 16, 2018 16:44
Show Gist options
  • Save luizbills/6c15cb472350d30410f1673ab4332f79 to your computer and use it in GitHub Desktop.
Save luizbills/6c15cb472350d30410f1673ab4332f79 to your computer and use it in GitHub Desktop.
very simple template renderer
function renderTemplate(template, data) {
var result = template;
for (var key in data) {
if (data.hasOwnProperty(key)) {
var regex = new RegExp('{{\\s*' + key + '\\s*}}', 'g');
result = result.replace(regex, data[key]);
}
}
return result;
}
// Usage
var template = 'hello {{ name }}!'
var result = renderTemplate(template, {
name: 'John'
});
console.log(result); // => hello John!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment