\"A\":\"\", \"B\":\"\", \"C\" /> \"A\":\"\", \"B\":\"\", \"C\" /> \"A\":\"\", \"B\":\"\", \"C\"/>

Comma between optional statements

136 Views Asked by At

I am trying to generate code with stringtemplate4, and I have a line like this:

"<if(A)>\"A\":\"<A>\"<endif>, <if(B)>\"B\":\"<B>\"<endif>, <if(C)>\"C\":\"<C>\"<endif>"

so A, B and C are optional parameters that if they are not set, I don't want the whole statement to be in the output string. That part is working fine but the comma after the statement is my problem, I don't want the comma to be there as well. I can have the comma inside the string (like "," but then I might end up with an extra comma at the end. I think one way is to do it like a list but I don't know how to define that. this is not working:

"<[{<if(A)>\"A\":\"<A>\"<endif>,<if(B)>\"B\":\"<B>\"<endif>}]; separator=\",\">"

Any ideas? thanks.

2

There are 2 best solutions below

1
Heiko On

Not sure if I understand your question correctly, but can't just move the comma in the if?

<if(A)>\"A\":\"<A>\", <endif><if(B)>\"B\":\"<B>\", <endif><if(C)>\"C\":\"<C>\"<endif>
0
swaroop On

This template will do the trick

<if(A)> do A <endif>
<if(B)><if(A),<endif> do B <endif>
<if(C)><if(A||B),<endif> do C <endif>