Last active
January 12, 2016 01:12
-
-
Save jdfm/f951c596ec5581f85d6f to your computer and use it in GitHub Desktop.
simple templates
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
/** | |
* Use a simple object to create your html with variables | |
* and whatever code you need to build it up. | |
* | |
* You can take advantage of type coercion here to | |
* end up with the final strings. | |
*/ | |
var template = { | |
placeholder1: undefined, | |
placeholder2: undefined, | |
toString: function(){ | |
return '' + | |
'<p>' + this.placeholder1 + '</p>' + | |
'<span>' + | |
'<em>' + this.placeholder2 + '</em>' + | |
'</span>' + | |
''; | |
} | |
}; | |
template.placeholder1 = 'This is a paragraph!'; | |
template.placeholder2 = 'This is emphasized text!'; | |
console.log('constructor-casting', String(template)); | |
console.log('stringification', '' + template); | |
console.log('method-calling', template.toString()); | |
console.log(template); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment