Operators of Condition
These operators impose condition on your template output, it depends on the result of logical expression placed to the right of the operator’s body.
CTPP 2.x defines threeo operators of condition: <TMPL_if LOGICAL_EXPR>, <TMPL_elsif LOGICAL_EXPR> and <TMPL_unless LOGICAL_EXPR>.
All this operators evaluates logical expression to the result and according to it executes or not the further instructions. You can also use variables (local and global) and user defined functions inside of the operator’s body.
The common form of the operators of condition is the following:
<TMPL_if LOGICAL_EXPR>
Some instructions if result of LOGICAL_EXPR has true value.
<TMPL_elsif LOGICAL_EXPR2>
Some instructions if result of LOGICAL_EXPR2 has true value.
<TMPL_elsif LOGICAL_EXPR3>
Some instructions if result of LOGICAL_EXPR3 has true value.
......
<TMPL_else>
Some instructions if result has false value.
</TMPL_if>
<TMPL_unless LOGICAL_EXPR1>
Some instructions if result has false value.
<TMPL_elsif LOGICAL_EXPR2>
Some instructions if result of LOGICAL_EXPR2 has true value.
<TMPL_elsif LOGICAL_EXPR3>
Some instructions if result of LOGICAL_EXPR3 has true value.
......
<TMPL_else>
Some instructions if result has true value.
</TMPL_unless>
The branch of <TMPL_else> is not firmly binds, it means that the following notification is allowed: <TMPL_if LOGICAL_EXPR> Some instructions </TMPL_if>.
Thus the operator <TMPL_unless differs from the operator <TMPL_if in the executing some instructions if the evaluated value is false.
Example 2.1:
Template: "Hello, <b><TMPL_var username></b>!<TMPL_else>Wrong user name!</TMPL_if>
Parameter: username => "John"
Output: "Hello, John!"
Example 2.2:
Template: "<TMPL_if username>Hello, <b><TMPL_var username>!<TMPL_else>Wrong user name!</TMPL_if>
Parameter: not defined.
Output: "Wrong user name!"
The sense is different for the second operator:
Example 2.3:
Template: "<TMPL_unless username>Hello, <b><TMPL_var username></b>!<TMPL_else>Wrong user name!</TMPL_unless>
Parameter: "John"
Output: "Wrong user name!"
Copyright © 2003 - 2010 CTPP Dev. Team | http://ctpp.havoc.ru