Escape a dot in a StringTemplate attribute / property name?

780 Views Asked by At

I have the following Map with some attributes to be used in a template

Map<String,String> attributes = new HashMap<String,String>();
attributes.put("attr1.val","foo"); 
attributes.put("attr2.val","bar");

In the template, if I refer to them as follows:

<b>$attr1.val$: $attr2.val$</b>

StringTemplate assumes that I am dereferencing a property on attr1. Escaping the dot ($attr1\.val$) doesn't seem to work either. The documentation doesn't seem to provide a clue on how or if this is possible.

Do I just have to change my delimiter to something other than a dot?

2

There are 2 best solutions below

0
On BEST ANSWER

Glancing at the manual this is covered under Difficult Property Names:

Some property names cause parse errors because of clashes with built in keywords or because they do not match the rules for IDs as used by String Template. These difficult property names can be dealt with by quoting the property name in combination with the indirect property construct:

$person.("first")$ --- Build in keyword

$person.("1")$ --- non ID conforment name

Difficult properties names are quite likely to occur when dealing with maps. Map keys can be defined using arbitrary strings, including spaces and syntax characters used to defined templates themselves.

0
On

I guess changing the delimiter would be easiest. StringTemplate has no escape mechanism inside the $'s.