i have a pageblock section that appears multiple times on my visualforce page.Hence for optimizing the code, I thought to take that out of my visualforce page and place it as a apex:component instead wherever necessary.

However, t doing so, I'm facing a challenge on how to access some variables that controlls the logic of required={!variable1} or rendered={!variable2} from inside the apex:component as this two variables are get/set from my controller extension class and can be accessed from the visualforce page directly but not sure on how to access the same from visualforce component too. Any idea on that will be highly appreciated.

Below is the mock up code that I want to use--

             <apex:page standardController="ObjectAPIName" extension="className">
              <apex:pageBlockSection columns="2">
                <apex:outputField value="{!o.field1}" required="{!variable1}" rendered="{!variable3}"/>
                <apex:outputField value="{!o.field2}" required="{!variable2}" rendered="{!variable4}"/>

    public Class className{
      public variable1 {get;set};
      public variable2 {get;set};

My components field visibility is also dependent on the variables used in controller.


1 Answer 1


If these are only a couple of values you can just pass those in as apex:attribute values:

    <apex:attribute name="variable1" type="Boolean" required="true" description=""/>
    <apex:attribute name="variable2" type="Boolean" required="true" description=""/>

    ... {!variable1} ... {!variable2} ...


with this in the page:

<c:MyComponent variable1="{!variable1}" variable2="{!variable2}"/>

But if you have many controller extension values you want to reference, you can pass in a reference to the entire controller extension:

    <apex:attribute name="ext" type="className" required="true" description=""/>

    ... {!ext.variable1} ... {!ext.variable2} ...


with this in the page:

<c:MyComponent ext="{!extension}"/>

and though there may be a way to avoid this, I've always added a property to expose the reference to the controller extension like this:

public class className {
    public className extension { get { return this; }
    public Boolean variable1 {get;set};
    public Boolean variable2 {get;set};

You must log in to answer this question.

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