VisualForce: convert carriage returns to html line-breaks in a long text field

codeulike picture codeulike · Mar 26, 2010 · Viewed 17.3k times · Source

In Salesforce, if I'm binding a text field into a VisualForce page, whats a good way to convert the carriage returns in the text-field into HTML <br/> tags?

e.g. starting from something like this:

<apex:page standardController="Case">
  <apex:pageBlock title="Test">
      <p>{!case.Description}</p>
  </apex:pageBlock>                   
  <apex:detail relatedList="false" />
</apex:page>   

... if the Description is long with lots of carriage returns, how do I HTML-ify it?

(I guess this is a fairly easy question, and I'm sure I could google it, but to get the Salesforce community going on here I figure we need a few easy questions.)

edit: (Bounty added to try and generate some excitement)

Answer

TehNrd picture TehNrd · Apr 2, 2010

Try this:

<apex:outputField value="{!case.Description}"/>

Using output fields will maintain formatting automagically.