Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Template Directives > Conditionals

Conditionals

Often you don’t know exactly what output is required until you process the template. Perhaps your web site should be orange on certain days of the week, or maybe negative numbers should be displayed in red. The Template Toolkit has a number of conditional directives that allow your template to make decisions about what path to take.

A conditional controls execution of a block of code, based on the value of a variable. In the Template Toolkit, there are two main conditional directives: IF and SWITCH. In addition, there is the UNLESS directive, which is a negated IF.

IF, ELSIF, ELSE, and UNLESS

The primary directive for conditional execution is the IF statement. The basic syntax is:

[% IF test %]
    action
[% END %]

where action is executed only if test is true (the Template Toolkit’s definition of “truth” is explained later in this section). IF statements allow for an optional ELSE clause, which is executed if test is not true. There can be multiple test/action pairs as well; these are written using the ELSIF statement:

[% IF today =  = "friday" %]
    Yay! It's Friday!
[% ELSIF today =  = "monday" %]
    Yuck.  It's Monday.
[% ELSE %]
    ...
[% END %]

There can be any number of ELSIF clauses, including none. The ELSE clause is also optional. Because the IF directive defines a block, the END token is not optional.

The test clause can be any statement, even just a single variable name; the extreme case is a test clause of 1—i.e., always true. If the result of this statement is 0 or “” (the empty string), test is considered to be false; everything else is true. Variables that have not been assigned a value, either with DEFAULT or SET, are considered to be false (the value of an undefined variable is an empty string).

More complex statements are possible, such as the earlier example. test can be arbitrarily complex. Other than simple variable value, another common test is equality or comparison: what value does a variable contain? The notation = = is used to compare strings because = is used for assignment—it is an error to try to assign to a variable in an IF statement, to prevent subtle errors and hard-to-diagnose problems. Comparison operators include:

=  =          Test for equality
!=          Test for inequality
<           Less than
<=          Less than or equal to
>           Greater than       
>=          Greater than or equal to
&&, AND     grouping
||, OR      grouping
!, NOT      negation

Some of these make sense only for numbers, such as >, >=, <, and <=. NOT is used to reverse the meaning of a test:

[% IF NOT today %]
    Error! 'today' not defined!
[% END %]

There is a special version of IF that does exactly this: UNLESS.

[% UNLESS today %]
    ...

UNLESS is exactly equivalent to IF NOT, and often clarifies the intent of the condition (but can be more confusing when combined with ELSIF clauses, even though this is a syntactically legal thing to do).

AND and OR can be used to construct compound statements that might otherwise require nested IF blocks:

[% IF today =  = "Friday" AND time >= 1700 %]
    Go home!  It's the weekend!
[% END %]

Without grouping, this would need to be:

[% IF today =  = "Friday" %]
    [% IF time >= 1700 %]
        Go home!  It's the weekend!
    [% END %]
[% END %]

As you can imagine, this would get very tedious for blocks with many options.

SWITCH and CASE

The SWITCH directive makes writing long IF / ELSIF / ELSE statements easier when the test condition needs to be compared to a number of possible outcomes. SWITCH consists of a single statement, which is evaluated once, and a number of CASE statements, against which the evaluated value is compared. For example:

[% SWITCH today %]
    [% CASE "Monday" %]
    Hi ho, hi ho, it's off to work we go.

    [% CASE "Friday" %]
    Friday's here, almost time for the weekend!

    [% CASE [ "Saturday" "Sunday" ] %]
    It's the weekend! Party!

    [% CASE %]
    Ho hum, just another workday...
[% END %]

The value in today is compared against each successive CASE statement until a match is found; the contents of the matching CASE statement are processed, or the contents of the default CASE statement are processed if no match is found (if there is a default CASE statement, of course). An equivalent IF / ELSIF / ELSE block would look like this:

[% IF today =  = "Saturday" OR today =  = "Sunday" %]
    It's the weekend! Party!
[% ELSIF today =  = "Monday" %]
    Hi ho, hi ho, it's off to work we go.
[% ELSIF today =  = "Friday" %]
    Friday's here, almost time for the weekend!
[% ELSE %]
    Ho hum, just another workday...
[% END %]

The SWITCH statement is cleaner and there is less syntax to maintain. Most important, however, is that if the test statement requires computation instead of just variable comparison, the SWITCH will be more efficient and has less potential for side effects.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint