put jquery template in external file
I use jQuery template on my site, I would like t开发者_JS百科o put them all on a separate file. How do it do it?
Here is one example
<script type="text/template" id="request-template">......</script>
if you have a big application, i suggest you look into require.js in combination with the text plugin
require.js allows you to dynamically load your scripts when they are needed, the text plugin is ideal for loading text content files the same way you would use it on javascript modules. so you could put your templates in separate files, and list them as dependencies for your javascript, then they get loaded when the javascript needs them.
edit simple solution: put your script blocks in a separate html file, use jquery to load them in.
separatefile.html
<script type="text/template" id="user-template">user content here...</script>
<script type="text/template" id="form-template">form content here...</script>
<script type="text/template" id="mail-template">mail content here...</script>
your script:
$(function(){
$('#templates').load("/separatefile.html", function(){
// handle everything after you have your templates loaded...
});
});
精彩评论