Posted By

wildpeaks on 10/06/10


Tagged

plugin jquery templates


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

chodorowicz
wildpeaks


jQuery Templates: Two ways to define a template


 / Published in: jQuery
 

URL: http://www.wildpeaks.com

Important: this snipplet has moved to Github.

Both methods work even without the plus extension of the Templates plugin

  1. /**
  2.  * Method 1: Static template, using a SCRIPT tag.
  3.  */
  4.  
  5. //HTML:
  6. <script id="MyStaticTemplate" type="text/x-jquery-tmpl">
  7. <div>Hello ${hello}</div>
  8. </script>
  9.  
  10. //Javascript:
  11. var MyData = {hello: "world"};
  12. $("#MyStaticTemplate").tmpl(MyData).appendTo("#contents");
  13.  
  14.  
  15.  
  16. /**
  17.  * Method 2: Dynamic, from a string (which could be stored in an external
  18.  * file or even in a database]
  19.  */
  20. var MyData = {hello: "world"};
  21. $.template("MyInlineTemplate", "<div>Hello ${hello}</div>");
  22. $.tmpl("MyInlineTemplate", MyData).appendTo("#contents");
  23.  
  24.  
  25.  
  26. /**
  27.  * Note that static templates cannot be called using the dynamic method,
  28.  * therefore this would not work:
  29.  */
  30. $.tmpl("MyStaticTemplate", MyData).appendTo("#contents");

Report this snippet  

You need to login to post a comment.