0

I have a dynamically generated HTML code in apex.

I need to put and render this HTML in a custom VF page.

How that can be achieved?

If I need an <apex:outputLabel> please advise how to put the dynamically generated HTML code in the <apex:outputLabel>?

Or do I need some other VF component such as <apex:pageBlockTable>? Please advise.

My dynamically generated HTML code is generating a table such as: enter image description here

Which VF component is the most appropriate to generate such table?

That is my VF page:

<apex:page standardController="Lead" extensions="LeadDuplicateController" tabStyle="Lead"> 
  <apex:pageBlock >
        <apex:pageBlockSection title="Lead Information">
            <apex:outputText value="{!plainHTML}" escape="false" />
        </apex:pageBlockSection>
  </apex:pageBlock>
</apex:page>

AND That is the controller class to the VF page:

public with sharing class LeadDuplicateController {

    Lead objLead;

    public String plainHTML { get; set; }

    public LeadDuplicateController(ApexPages.StandardController controller) {
        objLead = (Lead)controller.getRecord();
    }
}

My HTML code is in another class and another method.

How from that other method I can invoke the setter method in the LeadDuplicateController so that value of the HTML to be visualized and displayed in the VF page?

  • What is your reason for generating HTML, presumably by building a giant string, in Apex? Why not just provide the data to the VF page and let the VF page generate the HTML? – Mark Pond Jan 16 at 23:36
2

You would use apex:outputText to drop plain HTML in to the page:

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

Please note that you should sanitize the output to avoid script injection attacks if such a possibility exists by using String.escapeHtml4 on any user strings that you merge into the HTML.

  • thank you so much for your advise sfdcfox. By plain HTML do you have in mind pure and static HTML? If the {!plainHTML} is a variable how to put my HTML code generated in apex into plainHTML? – lemdev Jan 16 at 23:11
  • @lemdev no, plainHTML is a variable, such as public String plainHTML { get; set; }, and you would set it by whatever means necessary. – sfdcfox Jan 16 at 23:14
  • I've included the code of my VF page as well as of my controller class. I need to know how from that other method I can invoke the setter method in the LeadDuplicateController so that value of the HTML can be visualized and displayed in the VF page? – lemdev Jan 17 at 0:34
  • My HTML code is in another class and another method. I need to know how from that other method I can invoke the setter method in the LeadDuplicateController so that value of the HTML to be visualized and displayed in the VF page? – lemdev Jan 17 at 8:48
  • @lemdev You would just call the method from your constructor or an action method. Exactly what you'd do depends on if the method is static or not, etc. – sfdcfox Jan 17 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.