+ Start a Discussion
MiddhaMiddha 

Adding a new line / linefeed in Visualforce

Hi,

I am creating a string in my APEX class which needs to have new line characters in it. For eg:

"This is line 1.
This is line 2."

I need to show this on my Visualforce page. It works but the new line char does not appear on the Visualforce page. I have tried adding almost all types of linefeed chars possible but none worked.

When i use "This is line 1 \n This is line 2", it shows the same in Visualforce page without converting it to a  new line. I have tried \n,\r,\n\r,<br/>,&lt;br/&gt; and few others. I also tried setting escape attribute of "outputText" on visualforce but it didnt worked. 

Is there any way to make it work.

Thanks
[GM]
Jon Mountjoy_Jon Mountjoy_
This works for me:

Controller:

public String getStr() {
return 'Hello
New Line';
}

Page:



The docs for escape read:
A Boolean value that specifies whether sensitive HTML and XML characters should be escaped in the HTML output generated by this component. If you do not specify escape="false", the character escape sequence displays as written. Be aware that setting this value to "false" may be a security risk because it allows arbitrary content, including JavaScript, that could be used in a malicious manner.

Regards,
Jon
hokusaihokusai
I tried to implement this and got the error "line breaks not allowed in string literals"


Message Edited by hokusai on 11-03-2008 08:42 PM
Jon Mountjoy_Jon Mountjoy_
Sorry hokusai, somehow my output got munged.

You can do this if your Visualforce looks like this:

<apex:outputText value="{!str}" escape="false"/>

 and your controller like this:

 public String getStr() {
        return 'hello <br>foobar';
    }

 
Note that this does expose a security risk if you don't escape.  For more info, check out this article.

Regards,
Jon
hokusaihokusai
Thank you very much.


Message Edited by hokusai on 11-12-2008 06:49 AM
DownstairsBDownstairsB

Why that's simple:  If you want a line-break, simply concatenate the function br() into your text.

 

"This is Line 1"&br()&

"This is Line 2"

 

produces the value:

This is Line 1

This is Line 2

 

 

Priya_MarupudiPriya_Marupudi

&br()& is not working