Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested loop in StringTemplate

Im interested in writting something similar to a nested loop using StringTemplate template engine. In C# have a HashTable of which each Key contains List of Document objects, each Document has a title and source. I would like to list at the beggining of an email, a summary of the document titles per source.

<h1>Summary</h1>
<h2>Source A</h2>
<ul>
  <li>title 1</li>
  <li>title 2</li> 
</ul>
<h2>Source B</h2>
<ul>
  <li>title 3</li>
  <li>title 4</li> 
</ul>

What is the best way to accomplish this with StringTemplate?

like image 965
Benjamin Ortuzar Avatar asked Feb 24 '10 12:02

Benjamin Ortuzar


1 Answers

Assuming you've transformed these to appropriate data structures -- Source class having getName and getDocuments methods, and Document class having getTitle method, it will look like this:

$
sources:
 {
    source|
    <h2>Source $source.name$ </h2>
    $
    source.documents:
     {
      document|
      <li>title $document.title$</li>
     }
    $
 }
$
like image 146
Marat Salikhov Avatar answered Oct 21 '22 21:10

Marat Salikhov