Creating conditions for template elements

Use the condition editor to define a JavaScript expression with the data attributes and variables accessible in the current context.

Procedure

  1. Right-click an element and select Data > Edit condition.
  2. In the Set Condition for Current Element window, expand Variables and Attributes.
    Tip: Only the first level queries and attributes of a query can display as available. For secondary levels, you must add another query to an element and add the condition to that element.
  3. Enter the JavaScript code by using one of these options:
    • Write a script manually in this window
    • Copy and paste a script from an outside editor into this window
    • Click Include script and selecting a file from your computer, a remote server, or from the Central Management component
    • Use the condition editor:
      1. Select an attribute or variable for the Left operand.
        Note: If you use the tools in the Set Condition for Current Element window, any data attribute or variable that is used is automatically selected in the tree view. If you enter the condition manually, you must manually select the data attribute or variable.
      2. Select an Operator.
      3. Select an attribute or variable for the Right operand from the dropdown menus.
        Note: When the left operand and right operand of a condition are from the same attribute name, but belong to two different queries, the condition does not work. You must set different aliases for the two attributes for the condition to work.
      4. Click Add. The selected condition is added in the text box.
  4. Optional: Select the XHTML Input option to allow the XHTML tags contained in the script to be edited. If you are copying rich text or using rich text in conditions, you must select the XHTML Input option.
    Important: Invalid XHTML causes document generation to fail. Verify that your XHTML is valid before saving your changes.
  5. Optional: Click Test and Run to test the condition before applying it to the current element. If the script runs correctly, true displays. If the script does not run correctly, false displays.
  6. Click OK.

Results

When you apply a condition to an element, the element displays with an Condition icon icon in the template content editor:

Conditions icon on a Paragraph element

Examples: Constructing conditions with the condition editor

Review these examples to help you construct your own conditions with the condition editor.

Objects that have Object Headings in a Rational® DOORS® schema:
  • Expand Attributes > query.
  • Select [Object Heading] Object_Heading.
  • For the left operand, select [Object Heading] Object_Heading.
  • For the Operator, select Not equal to.
  • For the right operand, do not enter any value.
  • Click Add. The JavaScript displays as Object_Heading != "".
High priority requirements in a REST schema:
  1. Expand Attributes > query.
  2. Select Priority.
  3. For the left operand, select Priority.
  4. For the Operator, select Equals.
  5. For the right operand, enter High.
  6. Click Add. The JavaScript displays as Priority == "High".
Remove duplicate values from an attribute by creating a variable and setting this condition on the element:
  1. Expand Variables > variable.
  2. Select the variable.
  3. For the left operand, select the variable.
  4. For the Operator, select Equals.
  5. For the right operand, do not enter any value.
  6. Click Add. The JavaScript displays as variable == "".

What to do next

If you find that a variable is not evaluating correctly, check to see whether the variable is a literal string true or false, such as variable == "true".

In some cases, the value of a variable evaluates to a boolean value rather than the literal string value. Example: variable == "true" incorrectly evaluates to false if variable is the boolean true and not the string "true". Instead of looking for the existence of any value, the word true is not found in the data as a value. This issue occurs when a variable comes from a computation and not from either a document specification where it was provided when the report was configured or from a template where it was specified as the default value of the variable.

To ensure that the variable evaluates correctly, revise your condition by using one of these options:
  • Option 1: Enable your condition to handle both the literal string and the boolean value.

    (variable == boolean_true_or_false) || ( variable == "string")

    Example: (variable == true) || ( variable == "true")

  • Option 2: If you know in advance that the variable is to evaluate either as a boolean or string value, you can design the condition in anticipation of that value type.
    • Boolean value: variable == boolean_true_or_false

      Example: variable == true

    • String value: variable == "string"

      Example: variable == "true"

Creating an alias for attributes with the same names

In a scripted filter or condition, when the left operand and right operand of a condition are from same attribute name, but belong to two different queries, the condition does not work. You have to set different aliases for the two attributes for the condition to work.

Before you begin

Edit the Document Studio preferences:
  1. Rational Publishing Engine Document Studio, click Window > Preferences.
  2. Expand RPE and click Document design.
  3. In the Use Schema annotations for display field, select No and click OK.

Procedure

To differentiate attributes with the same name, you can use the Alias feature to eliminate ambiguities. When you are adding the condition that verifies that the module description equals the description of the DOORSObject, right-click the attribute to set the alias.

Results

Now the Query/Object/Object Text attribute is known as $1_Object Text and the condition can be processed correctly.

What to do next

Tip: To examine how the condition is applied to the data, click Test. In the Test window, click Run.

Feedback