title: Stream HTTP response of parsed template including loops
body: <p>I have a database containing millions of items. These items are to be exportable via web interface. The customer can specify a template (string) with placeholders on how to export the data.</p>
  
  <p>Since the datasets are vey large, I cannot do a simple search/replace on the template and send the result as a response, since this would eat away my memory. So I stream the response via Transfer-Encoding: chunked.</p>
  
  <p>A template may look like this:</p>
  
  <pre><code>Some static content at the beginning …
  
  [foreach item]{item.id} {item.name}[/foreach]
  
  Some static content in between the loops …
  
  [foreach item]{item.id} {item.value}[/foreach]
  
  Some static content at the end …
  </code></pre>
  
  <p>How can I stream this sequentially? If there was only one foreach loop, I could simply divide the streaming process in three stages:</p>
  
  <ol>
  <li>Stream content before loop line by line.</li>
  <li>Iterate through loop and stream each item line by line.</li>
  <li>Stream content after loop line by line.</li>
  </ol>
  
  <p>However, the template may contain 0..n foreach loops (based on the same item list). Is there a regex to extract all loops and the contents in between, so I can iterate over them?</p>