Ternary operation

Hi,

In the server action formatting menus, there are two operations that look much the same. I would like to understand the difference between them and when do I use each.
To access them, one must select a variable to bind, click on the formatter, and then right click on the variable name.

The first one is inside the Operation submenu. There are two options “?” and “:” that allow you to implement a ternary operation.

The other one is inside the Conditional submenu, and is call “Then”.

Any information on them would be appreciated.

Many thanks,

Alex

The structure of a ternary operator is
variable = value ? true result : false result

The structure of an if-then-else statement is
if variable = value
then true result
else false result

As you see, the difference is the ternary operator is an expression where as an if-then-else is a statement.

An expression returns a value and a statement doesn’t so the ternary operator can be used in places that an if statement can’t.

1 Like

I’ve only vaguely noticed the ‘Then’ menu option. It wasn’t obvious how it works (at least to me), until I clicked it after @ben’s comment above. Now it makes sense - though the syntax looks slightly odd and unfamiliar:

image

At least, I think it makes sense. I can see it being a useful feature, so did a quick test. Based on this info icon:

image

I entered values to be returned:

image

… but it only returns ‘true’ or ‘false’. I was puzzled by the result:

image

. but it works if the ID is converted to a number (which obviously isn’t required in the case of test_1).

Given that it seems only ‘true’ or ‘false’ is returned, I’m not sure what the purpose of this option is.

Tom, your syntax is not correct.
should be:

{{(category _id > 3).then('gt 3', 'lt 3')}}

as you want to apply the .then part to the whole condition… category_id > 3

2 Likes

Thanks @Teodor! All works as expected now.

Thank you for your answers and sorry for the late reply – I’ve been tied up and have not been able to look into it until now.

I’m not sure I understand the difference(s) between the two.

I understand the difference between an expression and a statement, but I’m not sure that the conditional “.then” that appears in the Formatter menu allows me to put an actual statement. The options presented are for expressions.

I believe that the one that allows me to put conditional statements in a server action is the one you access using the “Core Actions > Condition” .

So, frankly, I’m not sure what is the use case for the Formatter > Conditional > Then

I know I don’t get it and perhaps it’s because I am reading this the wrong way, but I think that the examples provided are used to set the value of a variable, which is the typical use case for the ternary operator.

Any further clarification on this point will be appreciated.

Many thanks in advance!

Alex

1 Like