Newer Version Available

This content describes an older version of this product. View Latest

apex:variable

A local variable that can be used as a replacement for a specified expression within the body of the component. Use <apex:variable> to reduce repetitive and verbose expressions within a page.

Use this component to get user input for a controller method that does not correspond to a field on an sObject. Only <apex:inputField> and <apex:outputField> can be used with sObject fields.

Note: <apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.

Example

1<!-- For this example to render properly, you must associate the Visualforce page 
2with a valid contact record in the URL. 
3For example, if 001D000000IRt53 is the contact ID, the resulting URL should be: 
4https://MyDomain_login_URL/apex/myPage?id=001D000000IRt53
5See the Visualforce Developer's Guide Quick Start Tutorial for more information. -->
6         
7<!-- Page: -->
8<apex:page controller="variableCon">
9    <apex:variable var="c" value="{!contact}" />
10        
11    <p>Greetings, {!c.LastName}.</p>
12</apex:page>
13
14/*** Controller ***/
15public class variableCon {
16    Contact contact;
17
18    public Contact getContact() {
19        if (contact == null){                      
20        contact = [select LastName from Contact where 
21            id = :ApexPages.currentPage().getParameters().get('id')];
22        }
23        return contact;
24    }
25}

Attributes

Attribute Name Attribute Type Description Required? API Version Access
id String An identifier that allows the component to be referenced by other components in the page. 10.0 global
rendered Boolean A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true. 10.0 global
value Object The expression that can be represented by the variable within the body of the variable component. Yes 10.0 global
var String The name of the variable that can be used to represent the value expression within the body of the variable component. Yes 10.0 global