0

I'm new to StringTemplate4 and probably I am going to ask something overly simple, impossible or stupid but I couldn't find any other information on it. So far, I have set this minimal set of templates:

define(name,arity) ::= "<name>(<vars(arity)>)."
vars(n) ::= "<n:var();separator=\", \">"
var(n) ::= "V<n>"

and I would like to get:

pred(V1, V2, V3).

by calling the following code:

STGroup group = new STGroupFile(...);
ST st = group.getInstanceOf("define");
st.add("name", "pred");
st.add("arity", 3);
String result = st.render();

Is it possible? Many thanks in advance.

Stefano Bragaglia
  • 622
  • 1
  • 8
  • 25

1 Answers1

0

StringTemplate doesn't have built-in operators for repeating. Instead, you'll need to iterate, like described in the following question.

Is there anything like Enumerable.Range(x,y) in Java?

Keep in mind that you'll need to pass an Iterator<T> and not an Iterable<T> due to a current limitation in StringTemplate (the interpreter supports Collection<T>, but not Iterable<T>). If you want to use the built-in iteration variable i or i0, you could also pass an appropriately sized new Object[n].

Community
  • 1
  • 1
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Hi Sam, many thanks for the reply. Since there are no repeating operators, even a simple `int v[] = {1, 2, 3}` would do the trick, but probably the `Iterator` requires some little coding but uses less memory... Since I'll probably will have to pass a further array of strings whose arity is exactly the `n` in my example, how I am supposed to use the `` or `` to generate my sequence of variables? The only reference I could find is [here](https://theantlrguy.atlassian.net/wiki/display/ST4/StringTemplate+cheat+sheet) and I didn't quite get what to do... cheers! – Stefano Bragaglia Apr 14 '14 at 23:54