StringTemplate is there a way to convert an empty string to null?

1k views Asked by At

I have a situation where I need to utilise the wrapping and anchor functionality on a list of object attributes that have formatting.

I can create a list in string template and then strip out null values, if they are just direct property access. However I want to be able to conditionally format some of the attributes. For example, given the templates.

PrintItem(item) ::= <<
<item.Count> <strip([item.Name, item.Value, PrintVat(item.Vat), item.Id]); separator=""\n"", anchor>
>>
PrintVat(vat) ::= "<if(vat)>Total VAT <vat><endif>"

and an object with the values

Count = 2
Name  = Tasty Chicken
Value = £100
Vat   = £20
Id    = 1

would produce

2 Tasty Chicken
  £100
  Total VAT £20
  1

If I passed the same object but with a null value for Value it would produce

2 Tasty Chicken
  Total VAT £20
  1

becasue strip removes the null. However if i pass the first object with a null value for Vat then I get

2 Tasty Chicken
  £100

  1

Because a template always resolves to an empty string. So is there any work around to convert the empty strings to null?

The apparent workaround here StringTemplate 3: how to filter a list? does not work as it throws a compile exception on the template. Specifically it doesn't like 'nothing' as the value for a map. If I could explicitly map a value to null as in that post I would have what I need.

0

There are 0 answers