Your Page Title
🔍

    Understanding apex:dynamicComponent in Visualforce

    The <apex:dynamicComponent> tag allows you to create and render components dynamically at runtime using Apex code. This is useful when the component structure or type needs to be decided programmatically.


    Why Use <apex:dynamicComponent>?

    • Enables dynamic UI rendering based on logic, user input, or data.
    • Useful for reusable components where the structure is not known at design time.
    • Allows developers to control layout and behavior dynamically.

    Basic Syntax

    xmlCopyEdit<apex:page controller="DynamicCompController">
        <apex:dynamicComponent componentValue="{!myComponent}"/>
    </apex:page>
    

    Apex Controller Example

    apexCopyEditpublic class DynamicCompController {
        public Component.Apex.OutputText myComponent { get; set; }
    
        public DynamicCompController() {
            myComponent = new Component.Apex.OutputText();
            myComponent.value = 'This is a dynamically generated text!';
        }
    }
    

    Use Cases

    • Conditional UI Rendering: Display different layouts or components based on user role or input.
    • Reusable Page Templates: Build pages that adapt structure based on object type.
    • Form Builders or Configurable UIs: Let users design what they want to see.

    Important Notes

    • Components must be created using Apex’s Component classes (like Component.Apex.OutputText).
    • The componentValue attribute should point to an Apex property that returns a Component.Apex object.