${if} - the terp conditional

< Layout ${foreach} Statement >

Every template language needs the ability to perform conditional expansions. terp supports the ${if} and ${switch} statements as well as the conditional expression for this purpose.

Let's look at some examples:

This is a ${if(os.family!="windows")}non-${end}Windows machine.

In this very simple example, the text "non-" is only included if the machine on which the expansion takes place is not running a Microsoft Windows operating system.

An if-statement has a scope that includes everything between the embedded if statement and the matching end statement. If the if condition is satisified, nested text and statements are expanded, otherwise neither text nor embedded statements are expanded. If-statements support elif and else clauses, for example:

It's a ${if(sex=='M')}boy!${elif(sex=='F')}girl!${else}..., what?${end}

You can associate metadata with each clause and the specified metadata is valid for the scope of the matching clause only.

By the way, the example above would be much better suited for the switch statement.

When the problem only involves a choice between two text alternatives rather than the conditional execution of nested terp statements, you might be better off with the conditional operator inside an expression:

It's a ${ sex=='M' ? "boy" : "girl" }!


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

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

Commandline