Question

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.

Was it helpful?

Solution

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].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top