Skip to content

Instantly share code, notes, and snippets.

@lucamug
Last active December 8, 2016 20:54
Show Gist options
  • Save lucamug/ca3fd47cbd9387c41bafd714972bbc49 to your computer and use it in GitHub Desktop.
Save lucamug/ca3fd47cbd9387c41bafd714972bbc49 to your computer and use it in GitHub Desktop.
// Simple way to embed html in javasript
Array.prototype.joinHtml = function () {
return this.join('');
};
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
var html = [
'<div class="{classes}">',
'<p>{text}</p>',
'</div>',
].joinHtml().supplant({
classes: "class1",
text: "dummy text"
});
console.log(html); // output: <div class="class1"><p>dummy text</p></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment