Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to package multiple jQuery templates in single xHttpRequest?
    primarykey
    data
    text
    <blockquote> <p><strong>Update:</strong> after another day of digging into this issue, I have found that the current jQuery template lib provides no way to do this. <a href="http://encosia.com/2010/12/02/jquery-templates-composite-rendering-and-remote-loading/">this article</a> describes a good approach. </p> <p>I would still like to hear of any additional thoughts on doing this. The article linked above requires that the returned string of templates be inserted into the DOM. Seems as though leaving the DOM out of this would be ideal, and less overhead on the browser. Imagine a large page, with multiple composite templates that may not get used. Although, maybe because the templates are wrapped in a script tag, there is only a single DOM item per template? Come on, let's hear some thoughts...</p> </blockquote> <p>Using <a href="http://api.jquery.com/category/plugins/templates/">jQuery template libs</a>, what's the best way to combine multiple, related, relatively small templates together? Do you need a single <code>&lt;script&gt;</code> tag for each individual template? What about in the case of dynamically pulling these templates via AJAX? Can I combine these templates somehow?</p> <p>Consider the following:</p> <pre><code>&lt;script id="movieTemplate" type="text/x-jquery-tmpl"&gt; {{tmpl "#titleTemplate"}} &lt;tr class="detail"&gt;&lt;td&gt;Director: ${Director}&lt;/td&gt;&lt;/tr&gt; &lt;/script&gt; &lt;script id="titleTemplate" type="text/x-jquery-tmpl"&gt; &lt;tr class="title"&gt;&lt;td&gt;${Name}&lt;/td&gt;&lt;/tr&gt; &lt;/script&gt; </code></pre> <p>Now because these two templates are very closely related (and one depends on the other) it would make sense to consolidate these into a single AJAX call, and get them both at once. I have a few ideas, but I'd like to know if there is common/best way to do this? Currently I pull in a chunk of HTML, and then do a .find() to get the specific peice of HTML for a template... e.g.:</p> <pre><code>var templatePackage = fancyAjaxCalltoGetTemplates(); </code></pre> <p>"templatePackage" might then look like this:</p> <pre><code>&lt;div id="templatePkg"&gt; &lt;div id="movieTemplate"&gt; {{tmpl "#titleTemplate"}} &lt;tr class="detail"&gt;&lt;td&gt;Director: ${Director}&lt;/td&gt;&lt;/tr&gt; &lt;/div&gt; &lt;div id="titleTemplate"&gt; &lt;tr class="title"&gt;&lt;td&gt;${Name}&lt;/td&gt;&lt;/tr&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I could then do:</p> <pre><code>var titleTemplate = jQuery.template('titleTemplate', $(templatePackage).find('#titleTemplate') ); </code></pre> <p>and</p> <pre><code>var movieTemplate = jQuery.template('movieTemplate', $(templatePackage).find('#movieTemplate') ); </code></pre> <p>...let me know what you think... what would you do?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload