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>
<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"/>
<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:
- The name attribute defines how the
custom attribute can be referenced in Visualforce pages.
names for attributes must be unique
within a component.
- The description attribute defines the
help text for the attribute that appears in the component reference library once
the custom component has been saved. The custom component is listed in the
reference library with the standard components that are also available.
- The type attribute defines the Apex
data type of the attribute. Only the following data types are allowed as values
for the type attribute:
- Primitives, such as String, Integer, or Boolean.
- sObjects, such as Account, My_Custom_Object__c, or the generic sObject
type.
- One-dimensional lists, specified using array-notation, such as String[],
or Contact[].
- Maps, specified using type="map". You don’t need to specify the map’s
specific data type.
- Custom Apex classes.
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.