salesforce
  1. salesforce-apex-attribute-component

apex:attribute Component

The apex:attribute component is a powerful feature in Visualforce that allows you to define attributes for a custom component and pass values to them. It is similar to using parameters in methods in a programming language.

Syntax

The syntax for the apex:attribute component is as follows:

<apex:attribute name="attributeName" type="attributeType" description="attributeDescription" required="isRequired" assignTo="{!assignedVariable}" />

Example

Consider the following example code:

<apex:component>
  <!-- Define an attribute for the component -->
  <apex:attribute name="headerText" type="String" description="The header text for the component" required="true" />

  <!-- Use the attribute to display the header text -->
  <1>{!headerText}</h1>
</apex:component>

To use this custom component with theheaderText` attribute set to a specific value, the following code be used:

<c:myComponent headerText="Welcome to my website" />

Explanation

In the example above, the apex:attribute component is used to define an attribute called headerText of type String. The attribute is marked as required using the required="true" attribute. The attribute is then used within the component to display the header text.

In the code that uses the custom component, the headerText attribute is set to a specific value, "Welcome to my website". This value is then passed to the custom component and displayed as the header text.

Use

The apex:attribute component is useful when you need to pass data to a custom component. You can define any number of attributes for a component, and pass values to those attributes when the component is used in Visualforce pages or other components.

Important Points

  • The name attribute is required for the apex:attribute component, while the type, description, and required attributes are optional.
  • The assignTo attribute can be used to assign the value of the attribute to a variable defined in the component controller. This is useful when you need to manipulate the value of the attribute before displaying it.
  • You can also define default values for attributes using the default attribute.

Summary

The apex:attribute component is a powerful feature in Visualforce that allows you to define attributes for a custom component and pass values to them. It is useful when you need to pass data to a component and can be used to define any number of attributes with optional types, descriptions, and requirements. The assignTo attribute can be used to assign the value of the attribute to a variable in the component controller, and default values can be defined using the default attribute.

Published on: