Nested loop in StringTemplate
Asked Answered
C

2

7

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?

Carlycarlye answered 24/2, 2010 at 12:7 Comment(0)
I
11

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>
     }
    $
 }
$
Idolum answered 22/3, 2010 at 14:10 Comment(2)
This is a good answer for general objects, but does not work for a HashTable.Carlycarlye
Could you show me a way how your hashtable and document objects look like? With hashtable, you may use ht.keys:{k|something} syntax.Idolum
B
0

There is a nice post that can help you to understand basics of StringTemplate:

Localizable text template engine using StringTemplate 4

Barmecidal answered 23/4, 2012 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.