Conditionals in Antlr String Templates
- by Pat Long - Munkii Yebee
We are using Antlr StringTemplates to give control over how a Entity's Name is output.
The basic Stringtemplate is
$FirstName$ $Initial$ $LastName$,
$Suffix$, $Degree$
I want to add some smarts to that template so that the commas are only output when necessary i.e. The first comma is only output when there is a Suffix or Degree and the second commas is only output if there is a suffix.
I tried the following template string bit it does not work. I guess I have misunderstood
$FirstName$ $Initial$ $LastName$
<if(Suffix|Degree)>,<endif>, $Suffix$
<if(Suffix)>,<endif> $Degree$
If it helps we process the templates using this C#
StringTemplate stringtemplate = new Antlr.StringTemplate.StringTemplate(template.Data);
foreach (Pair<string, string> pair in dictionary)
{
if (pair.First != null && pair.Second != null)
{
stringtemplate.SetAttribute(pair.First, pair.Second);
}
}
return stringtemplate.ToString();