开发者

Jquery Templates

开发者 https://www.devze.com 2023-01-25 14:42 出处:网络
I am using Jquery templates to display incoming JSON data I would like to load the template in a cacheable external file. How do I do this?

I am using Jquery templates to display incoming JSON data I would like to load the template in a cacheable external file. How do I do this?

UPDATE

http://encosia.com/2010/12/02/jquery-templates-composite-rendering-and-remote-loading/

very close to the solution I ended up with, I开发者_高级运维 just used a iframe instead;


These 2 pages seem to give the answer quite nicely:

  • jQuery Templates : Beyond The Basics : The Why And How
  • jQuery Templates : Create, Compile, Cache and Render


There seem to be several techniques to implement this each with its advantages/disadvantages.

1)Use a inline script block. This would bring the template blocks with each page load. If parent page is non-cacheable, it could get heavy with multiple templates

2)Access templates via a global variable object in an external javascript file. This is cacheable but template strings become hard to decipher

3)Template strings come as a part of JSON response. This begs the question, why not server side templating?

4)Use a static iframe and compile the templates on iframe load - this is cacheable, templates are readable/editable, same-origin policy could be a problem if static elements are placed on another domain

I finally opted for the iframe approach, but am too inexperienced to be aware of all pitfalls.

Thanks


I use ajax calls against server side resource, aspx in this example

$.ajax({ 
   url: "myprog.aspx", 
   data: { whichTemplate: "template I'm Looking for" }, 
   success: function(result) { 
                 // result is the text string containing either a single template or a delimated list of templates 
                 $.template('templatename', result); 
            } , 
   dataType: "text"
}); 


maybe this code can help:

<script id="entry_show_template" defer type="text/html">
  <div class="entry">
    Age: <span class="age"></span> <a class="name" href="#"></a>
  </div>
</script>

Maybe you could use this code also with src attribute, scripts are accessible so:

$('#entry_show_template') 


See my answer for jQuery templates - where should I put them?

In addition to the article from Dave Ward that you mentioned in your question, it references some additional tricks from An Introduction to jQuery Templates, by Stephen Walther. In particular, it covers how to fetch the template once and cache a compiled version of it.

0

精彩评论

暂无评论...
验证码 换一张
取 消