{if}
Sample Usage
{if: {time: YYYY; locale=en} - birthyear >= 18; trim=left} Welcome to our service!{else} Sorry! Since you are under 18 years old, you cannot use our service.{endif}
{formtext: name=x; default=10} {if: x > y}is greater than{elseif: x < y}is less than{else}is equal to{endif} {formtext: name=y; default=5}
Quick Overview VideoSettings
Setting Name Type Description Positional formula The condition to test. Must evaluate to yes
or no
. General Command Settings trim yes/no/left/right If yes
whitespace is removed before and after the command. If left
, only whitespace to the left is removed. If right
, only whitespace to the right is removed.
Setting Name | Type | Description |
---|---|---|
Positional | formula | The condition to test. Must evaluate to yes or no . |
General Command Settings | ||
trim | yes/no/left/right | If yes whitespace is removed before and after the command. If left , only whitespace to the left is removed. If right , only whitespace to the right is removed. |
The {if} command lets you conditionally include or exclude content based on a condition. That condition is a Text Blaze Formula that must evaluate to either yes
or no
.
Everything within the {if} command and the required {endif}
command will be inserted if the condition evaluates to yes
.
See also the {=} (Formula Command) command and the {repeat} command for additional tools to help you build dynamic snippets.
The required {endif} command
The if
command is similar to the {note} command in that the closing command must be present. In this case, it's the {endif}
command.
Everything inside of these two commands will or will not get rendered depending on the output of the condition, the if
command's positional setting.
The optional {else} command
What if you would like to do something different if the condition evaluates to no
?
In that case, you have access to an {else}
command. The {else}
command must be placed within the starting if
command and the closing {endif}
command.
There can only be one {else}
command.
Here's an example:
Customer: "I'd like a {formmenu: name=cappuccinoSize; small; regular} cappuccino please"
Server: {if: cappuccinoSize = "small"}"Not looking for much caffeine today? {else: trim=right} "Wow, that's adventurous... {endif: trim=right} Enjoy your {=cappuccinoSize} cappucino."
Multiple if conditions with the optional {elseif} command
It's possible to test for multiple conditions with the help of the {elseif}
command.
Similar to the {if} command, the {elseif}
also takes a condition as its positional setting that is a Blaze Formula.
Also, just like the {else}
command, they must be placed within the starting if
command and the ending {endif}
command.
You can have multiple {elseif}
commands.
Here's an example:
{if: number = "one"; trim=right} The number one. {elseif: number = "two"; trim=yes} The number two. {elseif: number = "three"; trim=yes} The number three. {else: trim=yes} It's neither one, nor two, nor three. It's some other number. {endif: trim=yes}
Here's what we're telling Text Blaze in the snippet above:
- If the form name called number contains the text "one", then give us the text "The number one"
- If the above isn't true, check whether it contains the text "two".
- If the above is true, then give us the text "The number two". If not, check whether it contains the text "three".
And so on.
At the very end, you'll need to end with an {endif}
command. The {endif}
command is always placed at the very end.
If the initial if
conditional evaluates to no
, Text Blaze will look for the next {elseif}
command and evaluate its condition. It evaluates to yes
, its contents will be shown, otherwise, Text Blaze will move on to the next {elseif}
.
If all {elseif}
commands within the if
command are evaluated to no
and there is an {else}
command, the contents of the {else}
command will be shown. The following examples illustrate the use of {elseif}
and {else}
.
Value: {formtext: name=val; default=10} (try changing to -5 or 20)
The number is {if: val >= 0}positive{else}negative{endif}
To be more precise it is {if: val > 0}positive{elseif: val < 0}negative{else}zero{endif}
Also, it is {if: val < -10}less than -10{elseif: val < 0}between -10 and 0{elseif: val <= 10}between 0 and 10{else}greater than 10{endif}
Dynamic content using the if
command
Text Blaze becomes incredibly powerful when you start combining different commands like {formmenu}, {if} and {import}.
For this example, try saving the following three snippets below into your Text Blaze dashboard. This will help you understand better how Text Blaze uses these functions by seeing them in action.
To copy the snippet examples to your dashboard, click the “Copy to Text Blaze” button in the top right of the interactive snippet.
First, save the snippet below and give it the shortcut /example1
.
There can be anything in here, including a
formtext
field like the one below:{formtext}
Now, save this second snippet below and give it the shortcut /example2
.
There can be anything in here, including a
formparagraph
field like the one below:{formparagraph}
Finally, save this third snippet below and give it any shortcut you please.
{if: options = "Example 1"}{import: /example1}{else}{import: /example2}{endif}
Now try running the third snippet somewhere in a text field.
Depending on the option you choose in the drop-down menu, Text Blaze will give you the contents of the respective snippet.
Now here's how that works.
- We created a drop-down menu with {formmenu} and gave it two options – Example 1 and Example 2. We also gave it the name "options".
- We created an
if
command and gave it the following instructions:- If the form name called options is equal to "Example 1", then you should import the contents of the snippet with the shortcut
/example1
- If the above is not true, then you should import the contents of the snippet with the shortcut
/example2
(that's where the {else} command comes in).
- If the form name called options is equal to "Example 1", then you should import the contents of the snippet with the shortcut
You don't have to use an {import} command when using {if} commands, but they can help you better manage your snippets.