4

I have a sObject that has a field called text__c.

I wish to display it on the visual force page.

The text will have line breaks in it.

Is there anyway for me to display this on a VisualForce page to include the line breaks, or must I break it up into multiple entries?

I was hoping that there would be a way to replace any set of characters, whether it is a /n or <br /> from the field with an actual <br/> html tag.

Thank you.

3 Answers 3

7

You can use apex:outputText with the escape attribute explicitly set to "false". Note however that this opens you up to security risks with injected content, such as JavaScript and other HTML tags.

2
  • 1
    Yes, definitely be wary about injection! You should consider having a whitelist of HTML tags and validate the input for those, rejecting all other tags. This probably isn't trivial in Apex, but would help security.
    – Mike Chale
    Commented May 2, 2013 at 11:31
  • Can we have some other option apart from setting escape="false", since it is considered as vulnerability in checkMarx report. Commented Mar 17, 2021 at 16:14
1

Do you really have <br/> there or newlines? I know it's a blasphemy but pure Visualforce isn't always the answer. And sorry but I'd double check my options before disabling escaping to achieve what I need...

Try with <pre> tag (it's abbreviation for "preserve" whitespaces and newlines I think). Or <code>. If you don't like the font (which most likely will become Courier New) - play with CSS to change the font.

Or experiment with white-space CSS property, for example an answer to https://stackoverflow.com/questions/4282757/how-to-make-word-break-on-firefox-using-css looks like a nice start.

-2

If your goal is to have the text "free flow" on it's own, if you use a controller extension, you could use string methods on each field record to strip the existing line breaks before the record is sent to the VF page for display.

The problem you'll have if you don't do this (or something similar) is that the existing line breaks will cause each line to break, then create a new line before the next line appears if the screen size isn't large enough. That's fine if you genuinely have new lines. If you have a paragraph, then that's not such a good thing when you're displaying it in on an html page where the window isn't large enough and it's not going to wrap properly.

Another option that comes to mind would be to create a list from each record using the line breaks to identify the split points you'd need in order to create separate entries that would comprise the list records. Once you have the list, you could then use Apex repeats to dynamically reassemble the field record for display on your VF page. By using the Apex repeats, the opening and closing <br> tags would already be there for you and not pose the kind of security danger associated with injection through unescaped html tags included in Apex:outputText.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .