Search code examples
htmlstringtemplate

How do you iterate over a map of lists using StringTemplate?


I need to produce HTML output that will look like this:

 - 2010
    - Item 1
    - Item 2
 - 2011
    - Item 9
 - 2012
    - item 6

Ive tried a map, ie Map<String,List<String>> but I cant work out how you would iterate over it, ie this doesnt work:

<ul class="chevron">
$x:{y|
    <li>$y$</li><ul class="chevron">
        $y:{z|<li>$z.name$/li>}$
    </ul>
}$
</ul>

Solution

  • In YAML notation, if the contents of the variable x (which is of type Map<String,List<String>>) are:

    x:
      2010: [Item 1,Item 2]
      2011: [Item 9]
      2012: [Item 6]
    

    The following will do what you ask for:

    <ul>
    $x.keys:{
    k | <li>$k$</li>
      <ul>
        <li>$x.(k);separator="</li>
        <li>"$</li>
      </ul>
    }$</ul>
    

    Note the $x.(k)$ notation.