+ Start a Discussion
Jon KeenerJon Keener 

Returning Visualforce markup from an APEX call and having it render correctly

I was experimenting this morning with trying to return Visualforce markup from a call in the controller.  It almost worked, but it appears that the variable returning the Visualforce markup was resolved after the rest of the APEX was resolved for rendering, so it didn't process the Visualforce. 
 
The purpose of my experimenting is to dynamically build a panelGrid with a differing number of columns (The columns including inputfields) based on prevously selected information on a previous screen of a wizard.
 
A snippet of the Page Code (in this case building the column heading row for a couple of fields):
Code:
<apex:outputLabel value="Shipping Plant" for="Shipping_Plant__c"/>
<apex:panelGroup>
    <apex:outputLabel value="Potential Annual Volume" for="Potential_Annual_Volume__c"/>
    <apex:outputLabel value="UOM" for="Potential_Annual_Volume_UOM__c"/>
</apex:panelGroup>
{!TectylLabel}
<apex:outputLabel value="AD Compound—" for="AD_Compound__c"/>
<apex:outputLabel value="OEM" for="OEM__c"/>

 
For testing, in my Controller, I put the following:
Code:
public String getTectylLabel() {
    return '<apex:outputLabel value="Tectyl Product—" for="Tectyl_Product__c"/>';
}

 
The resulting page ran correctly, but did not rendor the code above, but when I look at the source for that area of the page I see:
Code:
<TD><LABEL for=j_id0:j_id3:j_id4:j_id10:Potential_Annual_Volume__c>Potential 
Annual Volume</LABEL><LABEL 
for=j_id0:j_id3:j_id4:j_id10:Potential_Annual_Volume_UOM__c> UOM</LABEL></TD>

<TD><APEX:OUTPUTLABEL for="Tectyl_Product__c" value="Tectyl Product—" /></TD>

<TD><LABEL for=j_id0:j_id3:j_id4:j_id10:AD_Compound__c>AD Compound–</LABEL></TD>

 
What's interesting is that it did wrap the returned string in a <TD></TD>, but it just treated it as a string. 
 
There may be a way to do this that I'm missing.  I think being able to actually return Visualforce code from an APEX call would be an extremely powerful feature that would increase the ability to dynamically generate the interface.  I'm sure I'll find a way to do what I'm trying to do without this kind of functionality but I figured it would be good to post my findings in case someone else is trying this and has succeeded, or as an idea for future functionality.
 
Jon Keener

 
 
 
dchasmandchasman
We will be providing support for dynamic creation of components in a future release (currently slated for Summer '08) - using an approach that I expect you will like better than building strings of VF markup in apex code. Basically, you will have full access to the component API and its object model from within apex code. This will allow you to programmatically construct components, modify attributes, add/remove components from the component tree, etc.

Message Edited by dchasman on 12-20-2007 08:32 AM
RavishRavish
What is the current status of the feature (dynamic creation of components) Doug ? Or  where in the roadmap can we see this feature introduced?
AtnMAtnM
I would also like to make use of this capability. I looked for an idea I could vote up on the idea exchange but couldn't find one. Addmitedly I didn't search very hard mostly because I find the search in ideas returns a lot of irrelevent results and if I don't find what I'm looking for on the first or second page and the results don't seem relevant I don't have great confidence I'll find it on the next pages.