Frage

In StringTemplate v3 we had a custom Java HashMap implementation that would call a function on the key provided and return the function result instead of doing an actual hashmap lookup. We would add an instance of this HashMap as an attribute to stringtemplate. This would allow us to write custom functions in StringTemplate. We would call it like:

$myfunc.("argument")$

and

$somearray:{ v |
  $myfunc.("value: " + v)$
}$

The myfunc HashMap would here get a key of type String

Concatentation with + is not possible anymore in v4, and the proposed change is to make "value" + v into an anonymous template:

$somearray:{ v |
  $myfunc.({value: $v$})$
}$

In this case, a ST object is passed to the HashMap, and I can call 'render()' on it to generate the text I would usually get, BUT: the attribute 'v' is no longer passed from the surrounding scope. How can I, given an ST object find the surrounding scope (ST), so I can copy the attributes and make this work??

I really don't want to change my code to

$somearray:{ v |
  $myfunc.((v:{ v | value: $v$}))$
}$

because we have a big codebase, and this is a very disruptive change.

Is there any other way of acheiving what I want here? I am really trying to call a Java function with a string argument and have it return a value to the template, with the string argument being an expression that can access the attributes of the enclosing scopes.

War es hilfreich?

Lösung

You can use the "to string" operator (...) around your template:

$somearray:{ v |
  $myfunc.(({value: $v$}))$
}$
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top