The terp ${switch} statement

<${foreach} Statement ${import} Statement >

The switch statement is very useful when you need to conditionally expand a template based on a variable that can have many different values. Consider the following example:

It's a ${switch(sex)}${
    case('M','m')}boy!${
    case('F','f')}girl!${
    default}... what?${
end}

Please note how we are

  • using linebreaks inside the embedded statements to avoid linebreaks in the resulting text while providing structural hints to the template reader. Any whitespace inside the embedded statement's curly braces is ignored.
  • supplying multiple values (uppercase and lowercase) for the case statements.

If you have ever written code in Java or C-like languages, the switch statement will be immediately familiar. The biggest difference to the switch statement in programming languages is the absence of a break keyword. In terp, evaluation of the case clauses is aborted after the first matching clause has been evaluated. You can have one case match multiple values by providing a comma-separated list of values for the case statement.


Copyright 2006-2016 by Codemesh, Inc., ALL RIGHTS RESERVED

:
terp template statements
codemesh.com home expressions templates ant about us contact us download   

Commandline