replacete new line into <br />

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

replacete new line into <br />

user
Hello,

I want to let users in comment field (textarea) enter new line. After, I want to show their text in simple html page: span th:text="${valueString}". But this replaces new lines to spaces. How I can convert '\n' in ${valueString} to <br /> tag for browser rendering ?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: replacete new line into <br />

Emanuel
Administrator
You'll have to process the user input first, replacing \n with <br/> where necessary, before passing it back to the page to be rendered.  I don't think there are any special attributes or utility classes in Thymeleaf to do this for you.
Reply | Threaded
Open this post in threaded view
|

Re: replacete new line into <br />

user
I can replace in backend '\n' to '<br />'. But in front end I mus escape all HTML tags / so mine <br /> also will be escaped. What solution exists ?
Reply | Threaded
Open this post in threaded view
|

Re: replacete new line into <br />

Emanuel
Administrator
Oh, in that case there is an attribute, th:utext, that will not escape any HTML or XML code that you pass it.  Try replace th:text with th:utext.
Reply | Threaded
Open this post in threaded view
|

Re: replacete new line into <br />

danielfernandez
Administrator

Also note that the #strings utility object has a method called "replace(target, before, after)" you could use for performing your string modification operations during template execution.

Regards,
Daniel.