Defining and assigning variables

When designing a template, it might be necessary to calculate values or to make data attributes available in contexts where they are normally unavailable. For these situations, you can define variables and assign them values. The values can be static data or can be data available in the current context.

About this task

Variables can be used as placeholders for data calculated at runtime (variable assignments) or can be provided in the document specification. Unlike data attributes, you can also use variables in master pages.
There are two types of variables:
  • User-defined variables: User-defined variables are defined by the template designer. A user-defined variable can be internal or external.
    • Internal user-defined variables: Specify a variable as internal to perform calculations or to temporarily store information, such as a variable that is used to bring data into the master pages. Internal variables are not displayed in the document specification; therefore, you cannot provide them with values.
    • External user-defined variables: Specify a variable as external when you provide it with a value. External variables are displayed in the document specification. See Creating a document specification.
  • Rational® Publishing Engine variables: Rational Publishing Engine variables are internal variables available for use in the document template.
    Table 1. Internal variables
    Variable Description
    _element_id Kept internally in the template and used for debugging.
    _element_level The recursive level of an element.
    _row_number The row number in a current table. If the element is not a row the value is 0.
    _cell_number The cell number in a current row. If the element is not a cell the value is 0.

Procedure

  1. In the Outline view, click Variables > Insert > New Variable. The New Variable window opens.
  2. Enter the variable details in the following fields:
    1. Name: Enter the name of the variable. The variable name must use valid Java™ or JavaScript identifiers since they are used in scripts.
    2. Description: Enter the description for the variable.
    3. Default Value: Enter the value for the variable.
    4. Access: Select whether the variable is internal or external from the list.
      • Internal variables are not displayed in the document specification, so you cannot provide them with values.
      • External variables are displayed in the document specification and you can provide values to them.
  3. To assign a variable to an element:
    Tip: Assign variables to container elements whenever possible. The variable is applied to the elements within the container element. For example, you might want to use a variable within a table so that a new row is created for each variable. If you assign a variable to the table element, a table might be created for each variable. Sometimes these consecutive tables merge, but sometimes they display as separate tables. If you add a container element inside the table and drag the row into the container element, then assign the variable to the container element, a single table is created with rows for each variable.
    A table element with a container element. Inside the container element is a row element with three cell elements.
    1. Right-click the element and click Data > Edit Assignments. The Assignment Editor window opens.
    2. Click Add. The Select Variable window opens.
    3. Enter a name for the variable and click OK.
    4. Click OK in the Assignment Editor window.

Results

The selected variable is defined and assigned to the current element.

When you assign a variable to an element, the element displays an Variable assignment icon icon in the template content editor.

Paragraph element with a query and a variable assignment

What to do next

Tip: You can search for a list of variable assignments used in an element by right-clicking the element the variable is assigned to and selecting Search > Variable references in element. The Search view opens and a list of variable assignments displays. You can double-click a variable assignment in the results to edit it.

You can search for a list of variable assignments, attributes, and queries used in an element by right-clicking an element and selecting Search > All references in element. The Search view opens and a list displays. You can double-click an item in the results to edit it.


Feedback