I would like to know how to create a custom format function for string template. Let say I have the following code:
render(attributes) :: <<
<html>
$atributes: {
<div> $customformat(atribute.name)$</div>
}
</html>
>>
customformat(name) ::= <<
$name; format="upper"$
>>
Currently the behaviour of the function customformat is:
Input: "hello world" -> Output: "HELLO WORLD"
And I would like to modify the customformat function so the output is something like the following:
Input: "hello world" -> Output: "HELLO_WORLD"
As far as I'm aware this isn't possible, since StringTemplate is all about strict model-view separation.Instead, I think you'd be better off having a getter in the controller that returned the formatted string.
You might find this question useful: embed java code inside a template
Actually, I found a simple way of doing this which avoids the need for the formatted string getters:
You need to create a new StringRenderer which can format the string in the way you want.
Then you'll need to let the template group know about the new renderer:
Then you can call the format function like you did before, but pass "upperAndUnder" as the parameter:
which prints:
FYI:
Here's the original StringRenderer code
More info on Renderers