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

     

  • 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

      Maybe it needs to be case insensitive?

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

    • kavithams's avatar
      kavithams
      Guide

       

      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

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