IfStatementParser
Choose between two messages based on a comparison.
if compares two values and returns one of two messages. The payload holds both branches, split by a pipe.
For template authors
Syntax
{if(expression):true message|false message}Everything before the first unescaped | is the true branch. Everything after it is the false branch. Leave the false branch off to render nothing when the comparison fails.
Operators
| Operator | Example | True when |
|---|---|---|
== | a==a | The two sides match exactly. |
!= | a!=b | The two sides differ. |
> | 5>3 | The left number is larger. |
< | 4<8 | The left number is smaller. |
>= | 10>=10 | The left number is larger or equal. |
<= | 5<=6 | The left number is smaller or equal. |
Comparisons trim whitespace on both sides. The four numeric operators run both sides through parseFloat, so comparing text with > gives you NaN and a false result. A bare true or false works on its own. An expression with no operator at all counts as true.
Examples
Comparing against an empty value is the usual way to check whether a variable was filled in:
{if({args}!=):Hello {args}|You did not tell me your name.}For developers
Register the parser
import { Interpreter, IfStatementParser } from 'tagscript';
const ts = new Interpreter(new IfStatementParser());Behaviour
Both the parameter and the payload are required, so {if(1==1)} is declined and left in the output.
Only the first | splits the branches. Everything after it, pipes included, becomes the false branch. That is what lets you nest a second if inside the false branch, as in the example above.
To check several conditions at once, use any or all.
API reference
Last updated on