Search code examples
groovygsp

How to use replaceAll in gsp?


I have this code in my file:

<span class="highlighted-data-value">${address}</span>

address variable holds a value similar to:

Address line 1, City, Country

I want to line break on comas, but it looks like I am not able to do so.

Is there a way to do replaceAll inside that gsp file? Something like ${address.replaceAll(",","\\n")}

Ideally I would not want to use the controller, but to do it directly in gsp file.


Solution

  • ${address.replaceAll(",","\\n")} didn't produce expected result, because it broke String into 3 lines, but HTML renders it as a single line when there is no <br /> tag. Instead you can try breaking your String as:

    ${raw(address.replaceAll(',', '<br />'))}
    

    This will produce a result that should render an address broken to 3 lines:

    <span class="highlighted-data-value">Address line 1<br /> City<br /> Country</span>
    

    Alternatively you can split this String in controller, pass a list to a view and then use <g:each in=""></g:each> loop.