Last active
December 8, 2016 20:54
-
-
Save lucamug/ca3fd47cbd9387c41bafd714972bbc49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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