Skip to content

Syntax Using Conditional Tags in Radiant Pages

SwankInnovations edited this page Sep 13, 2010 · 17 revisions

The conditional tags <r:if> and <r:unless> both require a cond argument (short for “condition” or “conditional statement”). Writing this statement is the key to using the tags. For example:

<r:if cond="title = 'Home'">
<r:unless cond="content['extended'] empty?">

Conditional Statements

Conditional statements consist of (up to) three parts: a primary element, a comparison type, and (most of the time) a comparison element.

For example, in the conditional statement: 'testing' matches /test/,

  • the primary element is 'testing' (a string)
  • the comparison type is matches
  • the comparison element is /test/ (a regular expression)

There are two special comparison types – blank? and empty? – that work without any comparison element (as in content['body'] exists?). Notice that these comparison types end with a question mark. The sentence ends with them.

The Element Types

Both the primary element and comparison element follow the same rules – they’re written the same way. The Conditional Tags extension parses these elements and casts them as one of the following:

  • String (must be surrounded by single or double quotes like 'string' or "my string")
  • Regexp (must be surrounded by forward slashes like /abc|def/)
  • Boolean (the word true or false – case insensitive)
  • Nil (the word nil, null, or nothing – case insensitive)
  • Array (must be surrounded by brackets and can include any combination of the above types except regexp like: ['a string', 10, false, null])

Symbolic Elements

Rather than limiting you to literals, the Conditional Tags extension allow you to use symbolic elements. These provide a notation that can be evaluated at run time. Evaluators parse these elements and yield up a value to be compared in the conditional statement.

The Conditional Tags extension provides a mechanism for extension writers to build their own evaluators so that their own items can be used in conditionals.

The Comparison Types

  • exists?
  • blank? (or is-blank?)
  • gt (greater than)
  • lt (less than)
  • gte (greater than or equal to)
  • lte (less than or equal to)
  • equals (or is or = or ==)
  • matches (or =~)
  • includes (or includes-all) – compares two arrays. True if the comparison element is a subset of the primary element.
  • includes-any – compares two arrays. True if any of the comparison element elements can be found within the primary element