Blog

Output HTML to Visualforce from Apex

1

Sometimes its easier to format or ‘build’ a block of HTML from within your Apex class itself, rather than constructing something using the Visualforce components. Also at the time of writing there is a bug with the

<apex:repeat></apex:repeat>

component which inserts additional tags making it difficult to get the desired formatting. Check out this link for further details about this bug.

If you have ever passed formatted HTML from an Apex class to a Visualforce page, this probably resulted in the HTML outputted on the page rather than being processed by the browser in the way you had expected. The way around this is to use the escape=”false” attribute in your component to allow the HTML to be processed as you would expect.

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

I’ve used this method a couple of times when dynamically building tables inside the class rather than using VF components (depending on the requirements).

  • nizz

    Is there any other alternative for it?