kavithams
7 years agoGuide
Freemarker replace string operation does not work inside a function
We need to replace certain characters in a string in freemarker. Generally we do this.
<#assign message = message?replace("old","new") />
But this replace operation does not work when used with a function at all.
<#function replaceVal message>
<#assign message = message?replace("old","new") />
<#return message />
</#function>
The returned value does contain the new values when called.
Kindly help with the same.
kavithams - Small edit in your code and it worked fine. Strange but you have to use a different variable inside a function.
Before
<#function replaceVal msg> <#assign msg = msg?replace('following ','FOLLOWING') /> <#return msg /> </#function>
After
<#function replaceVal msg> <#assign repleacedText = msg?replace('following ','FOLLOWING') /> <#return repleacedText /> </#function>
Also, you if you want to ignore case-sensitive, you can pass another parameter 'i" as mentioned by PerBonomi
E.g
<#assign repleacedText = msg?replace('following ','FOLLOWING', 'i') />