1

We have a custom object called "Container" and each Container item is associated to a specific VF Component. On the Container record, you would select from a picklist the VF component it associates with to.

In the VF page, the component would be called based on this value.

In a perfect world, any component on any visualforce page could call the appropriate Container.

This is what I tried, but it didn't work:

<c:{!Container.Component_Name__c} />

I know that doing if{!Container.Component_Name__c} == 'this' do 'that' would work, but this is not ideal because there are many components!

thanks so much for your time

2
  • Is there a controller thats setting the component_name__c? how are you toggling the value of component_name__c field?
    – Rao
    Jul 16, 2014 at 19:10
  • @rao the component_name__c is just a value set on the container record, which is sent from the controller to the component. On the record, component_name__c is indeed the name of the component. Jul 16, 2014 at 19:13

1 Answer 1

1

This question has been asked previously here :

Loading a custom component into a Visualforce page dynamically

So this might (or) might not help you. I have introduced if loops but not sure if there is another way.

I am not aware of any way to dynamically construct an apex tag.

I took the approach of controlling the component to display in the controller where we have much control to do what we want.

Thanks to @sfdcfox. Based on this question I constructed the answer with a bit twist to accommodate what you are looking for:

https://developer.salesforce.com/forums?id=906F000000092Y5IAI

Page:

<apex:page controller="comp_test_controller">
 <apex:dynamicComponent componentValue="{!headerComponent }"/>
</apex:page>

Controller :

public class comp_test_controller {

    public transient ApexPages.Component headerComponent { get; set; }

    public comp_test_controller(){
        Type t = Type.forName('MyComponent');
        if(t != null) {
            for( ApexPages.Component cust : ((CustomComponentManager)t.newInstance()).newComponent()){          
                //In your case you will compare cust with the Component_Name__c value.
                if(string.valueof(cust) == 'Component.sitelogin'){
                    this.headerComponent = cust;
                }
            }
        }
    }
}

Class to support type.forname:

Previously type.forname('') used to support components, but it supports only apex class instances now.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_type.htm

    public class MyComponent implements CustomComponentManager {
    public List<ApexPages.Component> newComponent() {
        List<ApexPages.Component> comp_list = new List<ApexPages.Component>();
        comp_list.add(new Component.c.SiteLogin());
    return comp_list;
    }
    }

Interface:

global interface CustomComponentManager {
      List<ApexPages.Component> newComponent();
    }
4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .