Custom Component Attributes

Apart from standard Visualforce markup, the body of an <apex:component> tag can also specify the attributes that can be passed in to the custom component when it’s used in a Visualforce page. The values of such attributes can then be used directly in the component, or within the component’s controller, if applicable. They can’t, however, be used in the constructor of the component’s controller.

Attributes are defined with the <apex:attribute> tag. For example, the following custom component definition specifies two required attributes named value and textColor. Values for these attributes are referenced in the custom component definition using standard {! } Visualforce expression language syntax:

<apex:component>
    <!-- Attribute Definitions -->
    <apex:attribute name="myValue" description="This is the value for the component."
                    type="String" required="true"/>
    <apex:attribute name="textColor" description="This is color for the text."
                    type="String" required="true"/>

    <!-- Component Definition -->
    <h1 style="color:{!textColor};">
        <apex:outputText value="{!myValue}"/> 
    </h1> 
</apex:component>

Use this component in a Visualforce page with the following markup:

<c:myComponent myValue="My value" textColor="red"/>
An <apex:attribute> tag requires values for the name, description, and type attributes:

For information on additional <apex:attribute> attributes, see apex:attribute.

Default Custom Component Attributes

Two attributes are always generated for custom components. These attributes don’t need to be included in your component definition:
id
An identifier that allows the custom component to be referenced by other components in the page. If not specified, a unique identifier is generated automatically.
rendered
A Boolean value that specifies whether the custom component is rendered on the page. If not specified, this value defaults to true.