4

I store raw HTML code in a rich text field in the backend. I query it out in the controller and try to render it as HTML in a VF page. It displays the whole HTML code, it dosent render itself as HTML. I have used escape = false in the output text. This was working properly when the field was a long text area. Please provide me a way to get it to 'escape' once again.

3
  • 1
    why don't you try <apex:outputField> ?
    – Ratan Paul
    Feb 1, 2016 at 11:13
  • 1
    or you can go harder, use a hidden output field in visual force page, get value of the html field via JS, and apply that to certain page position. That's harder, but more flexible
    – kurunve
    Feb 1, 2016 at 11:15
  • You should post some code if you want a precise answer to why things aren't working. Oct 24, 2016 at 2:35

1 Answer 1

0

You can re-render the html code with JavaScript as shown in the example below. Rich_Text__c is a RichText field containing raw html code. Through jQuery the span tag is replaced by a div element with the raw html code created by jQuery. Now you can see rendered html in page.

VF Page

<apex:page controller="RichTextController" >
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <span id="inputtxt"/>
    <script type="text/javascript">
        $("#inputtxt").html($('<div/>').html("{!myText}").text());
    </script>    
</apex:page>

Controller

public class RichTextController {
    public String myText {get;set;}
    public RichTextController(){
        Book__c abook = [SELECT Rich_Text__c FROM Book__c where Id = 'a0Ii000000gKbw5']; 
        myText = abook.Rich_Text__c;
    }
}
7
  • 1
    This will not pass security review if its appexchange app Feb 1, 2016 at 11:29
  • @MohithShrivastava Thanks for your comment, I have updated my answer.
    – Saroj Bera
    Feb 1, 2016 at 14:09
  • 1
    This won't pass either.You have to HTML and JSENCODE your getter merge field Feb 1, 2016 at 14:18
  • Of course it can pass the Security Review. Rich Text fields are safe to render unescaped. Oct 24, 2016 at 2:34
  • I have tried and did not work for me
    – Nick
    Dec 15, 2016 at 21:53

You must log in to answer this question.

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