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
sean*harrisonsean*harrison 

databinding or including HTML

I'm playing with databinding and have a question that is probably obvious to more knowledgeable folks...

 

If I write a class like this:

 

public class HelloWorld
{
public String getHTML
{
get
{
return '<strong>The Earth says Hello</strong>';
}
set;
}
}

 

 

and create a page like so:

 

<apex:page controller="HelloWorld">
	<h1>Good morning, Starshine</h1>
	{!getHTML}
</apex:page>

 

I expect the output on my apex page to be something like:

 

Good morning, Starshine

The Earth says Hello

 

 

but instead I get:

 

Good morning, Starshine

<strong>The Earth says Hello</strong>

 

in other words my html tags are getting encoded so the databinding returns "&lt;strong&gt;...etc."

 

I want to send HTML to the page to be interpreted by the browser as HTML. Is there something I'm doing obviously wrong?

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

You can also use the visualforce outputText elements with the escape attribute set to false i.e.

 

 

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

 Cheers,

Wes

 

All Answers

Ispita_NavatarIspita_Navatar

You have included HTML tags in your get method :-

get
        {
            return '<strong>The Earth says Hello</strong>';
        }

instead any formatting please do it on the VF page.

 

Please try the following:-


<apex:page controller="HelloWorld">
    <h1>Good morning, Starshine</h1>
    <b>getHTML}</b>
</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

sean*harrisonsean*harrison

Thanks, but that doesn't really get at it.

 

If I want to send HTML or XML or whatever to the VF page and have it NOT encoded, is that possible?

 

Say, for example, I want to do a RESTful callout to some webpage, get the body, maybe do something with the DOM and then send it on to the VF page to be included. It's no good if the VF page always encodes every tag in the data.

 

sforce2009sforce2009

You can use a simple js like this

<apex:page controller="Test">
   <h1>Good morning, Starshine</h1>
   <span id="test"/>
   <script>
      document.getElementById('test').innerHTML = '{!getHTML}';
   </script>

</apex:page

WesNolte__cWesNolte__c

Hey

 

You can also use the visualforce outputText elements with the escape attribute set to false i.e.

 

 

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

 Cheers,

Wes

 

This was selected as the best answer
sean*harrisonsean*harrison

Ahhh...nice. Helps to learn all the params for the vf tags. Thanks!