Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with dynamic entries in StringTemplate

I'm using StringTemplate 4.0.8 with Java.

In the StringTemplate-4 documentation, it says that

Dictionary strings can also be templates that can refer to attributes that will become visible via dynamic scoping of attributes once the dictionary value has been embedded within a template.

How exactly do I do that? Can I do something like this:

output(input) ::= "The output is: <aDicitionary.input>"

aDictionary ::= [
    "someKey":"someValue",
    "someOtherKey":"someOtherValue",
    "aCertainKey": **HERE** i want the value to be <input>,
    default:"doesnt matter"
]

So that output("someKey") results in The output is: someValue and output(aCertainKey) results in "The output is: aCertainKey". If so, how exactly would the syntax look like?

I know that I could achieve the same by just not passing an input in one case and then checking if I have an input or not. But that would result in a lot of if's on the Java side which I

like image 944
Colophonius Avatar asked Dec 07 '25 23:12

Colophonius


1 Answers

To use a dynamic dictionary entry:

output(input) ::= <%The output is: <aDicitionary.(input)>%>

Use no quotes around the template and put input in parentheses to evaluate it.

To have dynamic content in a dictionary (the subject of the cited block):

aDictionary ::= [
  "someKey":"someValue",
  "someOtherKey":"someOtherValue",
  "aCertainKey": {input from scope <inputFromScope>},
  default:"doesnt matter"
]

Use braces around the keys and variable (or template) references inside. Now calling

<output(input="aCertainKey", inputFromScope="myInput")>

will output

The output is: input from scope myInput
like image 124
CoronA Avatar answered Dec 11 '25 10:12

CoronA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!