3

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;

Mr Hyde
  • 3,393
  • 8
  • 36
  • 48

5 Answers5

3

These 2 pages seem to give the answer quite nicely:

Veger
  • 37,240
  • 11
  • 105
  • 116
2

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

Mr Hyde
  • 3,393
  • 8
  • 36
  • 48
2

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"
}); 
Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
ethermal
  • 21
  • 1
1

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') 
mpapis
  • 52,729
  • 14
  • 121
  • 158
0

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.

Community
  • 1
  • 1
justis
  • 504
  • 4
  • 6