Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not going to be able to use <code>id</code> attributes to identify your templates without a bunch of server-side processing to take care of the uniqueness issues. But you can use classes to identify your templates.</p> <p>If you mash all your template HTML files together in override order ("base" templates first, "subtemplates" after) and use <code>class</code> attributes to identify the templates:</p> <pre><code>&lt;!-- templates.html --&gt; &lt;script type="text/template" id="fooTemplate"&gt; &lt;div class="foo"&gt; &lt;/div&gt; &lt;/script&gt; &lt;script type="text/template" id="barTemplate"&gt; &lt;p&gt;Bar!&lt;/p&gt; &lt;/script&gt; &lt;!-- newTemplates.html --&gt; &lt;script type="text/template" id="fooTemplate"&gt; &lt;ul class="foo"&gt; &lt;li class="bar"&gt;Blah!&lt;/li&gt; &lt;/ul&gt; &lt;/script&gt; </code></pre> <p>Then you can use things like</p> <pre><code>var foo = _.template($('.fooTemplate:last').html()); var bar = _.template($('.barTemplate:last').html()); </code></pre> <p>to access your templates.</p> <p>Demo: <a href="http://jsfiddle.net/ambiguous/gYHkF/" rel="nofollow">http://jsfiddle.net/ambiguous/gYHkF/</a></p> <hr> <p>You could also stick with <code>id</code>s and try to load templates from <code>newTemplates.html</code> first and fallback to <code>templates.html</code> if you don't find it. If you load the template files into two separate variables but <strong>don't insert them into the DOM</strong>:</p> <pre><code>var $base = $('stuff from templates.html'); var $subs = $('stuff from newTemplates.html'); </code></pre> <p>Then add a simple function to look for templates in <code>$subs</code> before <code>$base</code>:</p> <pre><code>function tmpl(id) { var $t = $subs.filter('#' + id); if($t.length) return _.template($t.html()); return _.template($base.filter('#' + id).html()); } </code></pre> <p>Then you could do this:</p> <pre><code>var foo = tmpl('fooTemplate'); var bar = tmpl('barTemplate'); </code></pre> <p>and The Right Thing would happen.</p> <p>Demo: <a href="http://jsfiddle.net/ambiguous/EhhsL/" rel="nofollow">http://jsfiddle.net/ambiguous/EhhsL/</a></p> <p>This approach also makes it easy to cache the compiled templates and not only avoid double lookups but avoid compiling the same thing over and over again:</p> <pre><code>function tmpl(id) { if(tmpl.cache.hasOwnProperty(id)) return tmpl.cache[id]; var $t = $subs.filter('#' + id); if($t.length) return tmpl.cache[id] = _.template($t.html()); return tmpl.cache[id] = _.template($base.filter('#' + id).html()); } tmpl.cache = { }; </code></pre> <p>Demo: <a href="http://jsfiddle.net/ambiguous/YpcJu/" rel="nofollow">http://jsfiddle.net/ambiguous/YpcJu/</a></p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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