I’m planning to use backbone.js and underscore.js for creating website, and I will have lots of underscore templates:
<script type="text/template" id="search_template">
<p id="header">
//header content will go here
</p>
<p id="form">
<label>Search</label>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
</p>
<p id="dynamic_date">
//dynamic data will be displayed here
</p>
</script>
Of course my templates will be much more complicated.
Since I will have lots of them, I don’t want to load all templates every time when page loads. I want to find a solution, where I can load specific template only when it will be used.
Another thing is that most of my templates will have same structure, only <p id="form"></p> and <p id="dynamic_date"></p> content will differ.
Could you please suggest me how should I do it?
Thanks,
Edit: I did some research and ported my iCanHaz code to underscore it also uses localStorage is available
Here is a github repository: https://github.com/Gazler/Underscore-Template-Loader
The code is:
Calling it would look something like:
Here is my original answer
Here is a method I wrote for ICanHaz (mustache) that performs this exact function for the same reason.
I then call it like so: