title: Create Jinja2 macros that put content in separate places
body: <p>I want to create a table of contents and endnotes in a Jinja2 template. How can one accomplish these tasks?</p>
  
  <p>For example, I want to have a template as follows:</p>
  
  <pre><code> {% block toc %}
   {# ... the ToC goes here ... #}
   {% endblock %}
  
   {% include "some other file with content.jnj" %}
  
   {% block endnotes %}
   {# ... the endnotes go here ... #}
   {% endblock %}
  </code></pre>
  
  <p>Where the <code>some other file with content.jnj</code> has content like this:</p>
  
  <pre><code>{% section "One" %}
  Title information for Section One (may be quite long); goes in Table of Contents
  ...
  Content of section One
  
  {% section "Two" %}
  Title information of Section Two (also may be quite long)
  
  &lt;a href="#" id="en1"&gt;EndNote 1&lt;/a&gt; 
  &lt;script type="text/javsacript"&gt;...(may be reasonably long)
  &lt;/script&gt; {# ... Everything up to here is included in the EndNote #}
  </code></pre>
  
  <p>Where I say "may be quite/reasonably long" I mean to say that it can't reasonably be put into quotes as an argument to a macro or global function.</p>
  
  <p>I'm wondering if there's a pattern for this that may accommodate this, within the framework of Jinja2.</p>
  
  <p>My initial thought is to create an extension, so that one can have a block for sections and end-notes, like-so:</p>
  
  <pre><code>{% section "One" %}
  Title information goes here.
  {% endsection %}
  
  {% endnote "one" %}
  &lt;a href="#"&gt;...&lt;/a&gt;
  &lt;script&gt; ... &lt;/script&gt;
  {% endendnote %}
  </code></pre>
  
  <p>Then have global functions (that pass in the Jinja2 Environment):</p>
  
  <pre><code>{{ table_of_contents() }}
  
  {% include ... %}
  
  {{ endnotes() }}
  </code></pre>
  
  <p>However, while this will work for endnotes, I'd presume it requires a second pass by something for the table of contents.</p>
  
  <p>Thank you for reading. I'd be much obliged for your thoughts and input.</p>
  
  <p>Brian</p>