Search Here

Custom Search

Saturday, June 7, 2008

Properly display paragraphs in browser

When we write something in several paragraphs in textarea of browser then it shows as it is in teaxtarea. But when we want to show the text in read only mode then the paragraphs loose their identity. I mean you can not separate the paragraphs from each other. So we need to replace the newlines of the text by
.

I have written a simple function named ‘keepWhiteSpace’ to do this conversion and I used it in many of Therap's forms to display the saved text as it is. Usage is very simple.

For example, if you want to show your text in a cell then write as follows:

${therap.keepWhiteSpace(myText)}

The implementation is very simple.

<#function keepWhiteSpace value>
<#return value?replace("\r\n","<br>")?replace("\n\r","<br>")?replace("\r","<br>")?replace("\n","<br>")>
</#function>

Ftl(Free Marker Tag Library) users can use it without rewriting any new one. JSP users need to write a tag using the logic.

Note that I haven’t replaced spaces by