function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tinman44tinman44 

Render HTML stored in field?

Hello all-
 
I am trying to display HTML stored in a field on a Visualforce page. But not sure it is possible. For example:
 
I have a custom object "HTML_Object__c" with a field called "". I want to call a custom component in a page that calls a custom controller that queries SFDC for this fields data:
 
Code:
public HTML_Object__c getHTMLText() {
      
        HTML_Object__c HTMLText= [select  from HTML_Object__c Where SOME CONDITION IS TRUE]; 
   
  return HTMLText;
    }     

 Then in the controller I want to display it in the VF page:
 
Code:
{!HTMLText.HTML_output__c}

The problem is it displays the HTML as HTML. So if I had "<strong>Test</strong>" stored in the field it would display exactly that and not "test".

Any one know if I can do this?? Thanks in advance


 

 
JeremyKraybillJeremyKraybill
Use the "escape=false" parameter.


Code:
<apex:outputText value="{!obj.yourHtmlField}" escape="false" />

 
HTH

Jeremy Kraybill
Austin, TX
tinman44tinman44

Thanks for the quick response!

I tried that and it is still rendering it as "<strong>test</strong>".

Am I just seeing a bug? Have you gotten that to work?

tinman44tinman44

I tried one more time and saw what I was doing wrong...I was putting it between the tags not in the value.

Code:
<apex:outputText escape="false">{!FIELD}</apex:outputText>


Once I moved it to this way it worked like a charm.

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

Thanks!
 
 

Ron HessRon Hess
Here you go:
Code:
<apex:page standardController="account" >

<apex:outputText value="{!account.description}" />
<apex:outputText value="{!account.description}" escape="false" />

</apex:page>

 shows

<strong>Text</strong>

Text



also check out the docs on outputText. 
You should be aware that this may allow non HTML to be rendered as well, including javascript, see the docs for more on this important detail.
vishesh91vishesh91

How to get the same for selectoption label

 

controller returns selectOption label as html

ShantinathShantinath

Thanks Jeremy!