title: underscore.js nested templates
body: <p>Is it possible to somehow take a DOM element from a underscore template and use it as another template?</p>
  
  <p>The idea is that my app needs to render a document that contains a loop with items and a summary. I need to occasionaly re-render only the summary or a few items, so I cannot just re-render the whole document.</p>
  
  <p>However, I would like to keep it simple for the app users to create their own templates for the document and I think that keeping everything in one file for a document would make it easier.</p>
  
  <p>I'm trying to use something like this:</p>
  
  <pre><code>&lt;script type="text/template" id="document-template"&gt;
      &lt;div id="document"&gt;
          &lt;h1&gt;&lt;%= name %&gt;&lt;/h1&gt;
          &lt;ul class="items"&gt;
              &lt;% _.each(items, function(item) { %&gt; 
                  &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
              &lt;% }); %&gt;
          &lt;/ul&gt;
          &lt;div id="summary"&gt;
              &lt;p&gt;Total items: &lt;%= totalitems %&gt;&lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>Now, I can easily do this var <code>documentTemplate = _.template($('#document-template').html());</code> to turn this into a document template, but I would like to turn the summary part into a template and a list item into a template as well.</p>
  
  <p>Can I do something like this:</p>
  
  <pre><code>var summaryTemplate = _.template($('#document-template #summary').html());
  var itemTemplate = _.template($('#document-template .items li').html());
  </code></pre>
  
  <p>PS. Actually I am loading the template from an external file using jQuery's $.get. This way I will get the document-template in one big string. From there, I can do just <code>documentTemplate = _.template(loadedString);</code>.</p>
  
  <p>Now, if I could just extract the #summary element from the string, it should work. But when I try to convert the string to DOM element  ( <code>var domElement = $(loadedString)</code>)
  (so I could do this: <code>summaryTemplate = _.template($('#summary',domElement).html());</code>, it won't work, because underscore won't recognize the &lt;%= %> tags anymore.</p>