Created
July 3, 2012 11:20
-
-
Save danscotton/3039157 to your computer and use it in GitHub Desktop.
refactoring our js templating using curl
This file contains hidden or 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
| ====== | |
| BEFORE | |
| ====== | |
| This is how we currently do 'JS templating' when we want to insert | |
| snippets of markup into the DOM: Strings of markup in an array, | |
| using [].join(''); We then do String.replace() to replace the | |
| {{placeholders}} with content. | |
| // minihyper.js | |
| define([ | |
| 'module/bootstrap' | |
| ], function( | |
| news | |
| ) { | |
| var numMiniHyperpuffStories = 4, | |
| miniHyperTemplate = [ | |
| '<aside class="pullout mini-hyper">', | |
| '<div class="pullout-inner">', | |
| '<h3 class="heading">{{title}}</h3>', | |
| '<div class="related-items">{{content}}</div>', | |
| '</div>', | |
| '</aside>' | |
| ].join(''), | |
| // ... | |
| // do stuff with template below | |
| // ... | |
| }); | |
| ===== | |
| AFTER | |
| ===== | |
| Alternatively, we can use curl and the text! plugin to reference a | |
| .html file. (We don't currently have this, so maybe we could | |
| rebuild to curl.min.js to include it...Tom?). This way, markup | |
| stays as markup without having to wrap it in nasty quotes. We | |
| can still keep the {{placeholder}} syntax as curl reads the html | |
| file in as a string. Thoughts? | |
| // minihyper.js | |
| define([ | |
| 'module/bootstrap', | |
| 'text!module/hyperpuff/minihyperTemplate.html' | |
| ], function( | |
| news, | |
| miniHyperTemplate | |
| ) { | |
| var numMiniHyperpuffStories = 4; | |
| // ... | |
| // do stuff with template below | |
| // ... | |
| }); | |
| // module/hyperpuff/minihyperTemplate.html | |
| <aside class="pullout mini-hyper"> | |
| <div class="pullout-inner"> | |
| <h3 class="heading">{{title}}</h3> | |
| <div class="related-items">{{content}}</div> | |
| </div> | |
| </aside> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From requirejs.org:
I tried using
text!for the mini-hyper template, but ran into the x-domain issue described above. I don't know whether we can setAccess-Control-Allow-Originon certain files for this?As for the compilation, it seems it just converts it to a string. But that's not a problem, right? It was a string before using
[].join()anyway...