Use obj.propertyValue to build format string?

13 views
Skip to first unread message

Alice Weintraut

unread,
Mar 14, 2016, 2:38:10 PM3/14/16
to stringtemplate-discussion
I'm referencing c# resource strings from my template (which uses $ as delimiters).  I'm doing so by passing in as one of the arguments to the template the object "strings", which is a c# ResourceManager.  The formatString is expected to be the name of the resource desired.  This allows me to put in my template for example, to get the localized version of ItemName:

    <Name>$strings;format="ItemName"$</Name>

The only issue with this solution is, there are times I want something from my object tree to help build the name of the resource string to look up.  For example, a generic lookup for a localized value for an enum could be provided with:

    <Type>$strings;format="ItemType$myItem.Type$"$</Type>

However - when I attempt to call the renderer this way, if I use the above syntax, the myItem.Type is not resolved into its value, and instead passed directly as part of the string, so I get the value "ItemType$myItem.Type$" rather than say, "ItemTypeBicycle".

If I don't place quotes around it however, such as:

    <Type>$strings;format=ItemType$myItem.Type$$</Type>

 I get a compile-time error that this "doesn't look like an expresson"

Is there another syntax I'm missing, or are you not able to resolve something that is then passed as a string like this?

AliceW

unread,
Mar 14, 2016, 3:32:00 PM3/14/16
to stringtemplate-discussion
I've found a solution myself - leaving this here in case it helps someone else.  The proper syntax is to escape with a { ...}, indicating an anonymous template.  The output being string is assumed, giving format the required type.

    <Type>$strings;format={ItemType$myItem.Type$}$</Type>

Reply all
Reply to author
Forward
0 new messages