Search code examples
stringtemplatestringtemplate-4

ST4 iterate a list with an index


So I have a list that is iterated over like so:

body(foo) ::= "<foo:{it|<\n><\n><bar(it)>}>"

bar(x) ::= "[<x.key>:<x.value>]"

I'd like to use the index.

bar(x) ::= "[<i0>:<x.key>:x.value>]"

I saw that there is an <i> and <i0> index token, but I don't understand how it is used, or if it could be used to do what I want to do.


Solution

  • Ok, so the trick is to pass <i> or <i0> in if you have a function, but i is implicitly available inside an iterator:

     body(foo) ::= "<foo:{it|<\n><\n><bar(i0,it)>}>"
    
     bar(i,x) ::= "[<i>:<x.key>:<x.value>]"