Search code examples
javaantlr3stringtemplate

increment a value in iteration in java stringtemplate


I have my array as below in java string template:

$colors:{ color |
<p>$color.name$
}$

Output is :

Green
Black

The output is want is:

1. Green
2. Black

How do I achieve this? How to get the incremented counter in antlr.stringtemplate ?


Solution

  • You can use built-in attribute $i$ to start enumeration from one, or $i0$ - from zero.

    $colors:{ color |
    <p>$i$.&nbsp;$color.name$
    }$
    

    Please, also see another example usage in ST3 GIT repo.