Reputation: 3629
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();
Upvotes: 1
Views: 2069
Reputation: 11
Just Use the Separator in you String Template like: $FirstName;separator=","$ $Initial;separator=","$
This might not be the exact answer but you can try using separator to separate the First Name with Initial and comma will be inserted only if you have second value. Say if you are iterating over a set of first names and initials then comma will come only if you have initials.
Upvotes: 1
Reputation: 26
Use $FirstName$ $Initial$ $LastName$ $if(Suffix)$, $Suffix$ $endif$ $if(Degree)$, $Degree$ $endif$
N
Upvotes: 1