Working with {if} statements with more than two options

Hi all,

I recently got a question from a fellow user, about how to use {if} and {then} when you have more than two options. To clarify, first I'm gonna give you the basic scenario with just two options, then I'll show you a more advanced scenario with 3+ options.

Basic scenario: Two options

Customer:
"I'd like a {formmenu: name=cappuccinoSize; small; regular} cappuccino please"

Server:
{if: cappuccinoSize=="small"}"Pathetic... can't even handle the real thing... {else: trim=right}
"Wow, that's adventurous... {endif: trim=right}
Enjoy your {=cappuccinoSize} cappucino."

Here's what's happening in this example.

  1. We have a form menu with the name cappuccinoSize and two options: small or regular
  2. So, cappuccinoSize is a variable that may contain one of the two options: small or regular
  3. In the if/then statement we're telling TextBlaze to look into the cappuccinoSize variable
  4. If TB finds the option small, it will return the first reply "Pathetic... can't even handle the real thing..."
  5. If it finds anything else, then it returns the second reply "Wow, that's adventurous... "

That's the basic scenario, namely: "If the supermarket has fresh milk, bring me 2 cartons, otherwise get the long-life one."

Now here's the more advanced scenario with multiple options:

Customer:
"I'd like a {formmenu: name=cappuccinoSize; small; regular; large; bathtub-sized} cappuccino please"

Server:
{if: cappuccinoSize=="small"}"Pathetic... can't even handle the real thing... {else: trim=right}
{if: cappuccinoSize=="regular"}"Wow, that's adventurous... {else: trim=right}
{if: cappuccinoSize=="large"}"Oooooh, you think you can handle that, mate? {else: trim=right}
{if: cappuccinoSize=="bathtub-sized"}"Yeah, guess you could use a bath... {endif: trim=right}{endif: trim=yes}{endif: trim=yes}{endif: trim=yes}
Enjoy your {=cappuccinoSize} cappucino."

Here's what's happening in this example.

  1. We have a form menu with the name cappuccinoSize and four options: small, regular, large, bathtub-sized
  2. So, cappuccinoSize is a variable that may contain one of the two options: small, regular, large, bathtub-sized
  3. In the if/then statement we're telling TextBlaze to look into the cappuccinoSize variable
  4. If TB finds the option small, it will return the first reply "Pathetic... can't even handle the real thing... "
    So up until this point, it's exactly the same thing as the first example.
    Thing change from the next step.
  5. If TB does NOT find small it goes into the next if/then statement where it checks to find the word regular. If it finds it, it returns the text "Wow, that's adventurous..."
  6. But if it does NOT find regular, it goes into the next if/then statement where it checks to find the word large. If it finds it, it returns the text "Oooooh, you think you can handle that, mate?"
  7. And finally, if it does NOT find large, it goes into the next if/then statement where it checks to find the word bathtub-sized. If it finds it, it returns the text "Yeah, guess you could use a bath... "

So, to give another supermarket-themed example:

"If the supermarket has fresh milk, bring me 2 cartons.
If it doesn't, check whether they have long-life. If it does, bring two cartons of that.
If it doesn't have long-life either, check whether it has powdered milk. In that case, bring a packet of that.
And if they don't even have powdered milk, put the place to the torch cos it's useless. :laughing:"

So, I hope this clarifies things a bit. If/Then statements can get pretty confusing when you start nesting loads of them, but eventually you'll get the hang of it. Just save the examples I gave above to your dashboard and use them as templates for when you need to create yours.

And if you have any question, feel free to ask :slight_smile:

By the way, just a few extra pointers to conclude:

Every if argument must:

  • start with an if statement that tells TB what to look for
  • followed by a result that TB should produce if the if condition is fulfilled
  • followed by an else statement
  • followed by a result that TB should produce if the if condition is NOT fulfilled
    followed by an endif statement

If you have multiple if arguments nested within each other, all of the endif statements must go at the very end of the collection. You can see this structure at work in the second example I gave.

Again, don't let any of this intimidate you. Once you've done it a few times, it becomes second nature.

1 Like