Forum Discussion

kavithams's avatar
7 years ago

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') />

     

6 Replies

  • kavithams - Can you please share dummy text which you trying to replace and couldn't replace with function?

     

    I tried to replace a string using your code and it worked without any issue. 

     

     

    <#assign message = "this is old value" />
    <#assign message = message?replace("old","new") />
    
    ${replaceVal(message)}
    
    <#function replaceVal message>
         <#assign message = message?replace("old","new") />
         <#return message />
    </#function>

    I got result "this is new value". 

     

     

    You can try this function here 

  • PerBonomi's avatar
    PerBonomi
    Boss
    7 years ago

    Maybe it needs to be case insensitive?

    ?replace("old", "new", "i")

  • kavithams's avatar
    kavithams
    Guide
    7 years ago

     

    TariqHussain Thank you for your reply.

    I'm sharing the the code lines below. I have this code within an endpoint between; if this info helps in anyway. The null variable "surveyUrl" uninitialised is intensional so as to throw a required exception to try "replace" with.

     

    <#attempt>

    <script type= text/javascript>

    window.location.href="${surveyUrl}";
    </script>

    <#recover>
    <#assign errorMsg = .error/>
    <#assign msg = errorMsg?replace('following ','FOLLOWING') />
    <#--<#assign msg = msg?replace("'","\'") />-->
    msg : ${msg}<br>

    <br>calling function replaceVal...<br>

    msg : ${replaceVal(errorMsg)}

    </#attempt>

     

    <#function replaceVal msg>

    <#assign msg = msg?replace('following ','FOLLOWING') />
    <#return msg />
    </#function>

  • TariqHussain's avatar
    TariqHussain
    Boss
    7 years ago

    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') />

     

  • kavithams's avatar
    kavithams
    Guide
    7 years ago

    Using a differently named variable inside function is really strange but your solution worked great for me. Thank you.

  • PerBonomi's avatar
    PerBonomi
    Boss
    7 years ago

    I just remembered. inside a function use <#local var = />, instead of <#assign var =.

    That shouldn't override the pre-existing variable and keep it inside the function.

    You know, in case you want to use the same variable name.