Skip to content

Instantly share code, notes, and snippets.

@designeng
Created April 19, 2016 07:57
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 designeng/7e6ca9a91a2caf1f481a073adf5162f1 to your computer and use it in GitHub Desktop.
Save designeng/7e6ca9a91a2caf1f481a073adf5162f1 to your computer and use it in GitHub Desktop.
/**
* Map object keys in string to its values
*
* @param {String} text
* @param {Object} data
* @return {String}
*/
template: function (text, data) {
return !text ? '' : text.replace(/\{([^\}]+)\}/g, function (value, key) {
return key in data ? data[key] : value;
});
},
@designeng
Copy link
Author

Usage:

var htmlSpan = '<span class="{className}">{content}</span>';
var button = utils.template(htmlSpan, {
      className: this.className('button'),
      content:   text
});

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