if

IF Statements

Neto's IF, IF THEN & ELSE function

Example

[%if [@somedata@] like 'apple' %]
    Apple
[%elseif [@somedata@] like 'orange' %]
    Orange
[%elseif [@somedata@] ne '' %]
    Data
[%else%]
    Nothing
[%/if%]

Usage

Simple Statements

Let's say we are working on a product template and want to display the message "Free shipping", but only on products where the price is greater than $200. Since the product will either have a price greater than $200 or less than $200, the “if” logic statement is the most appropriate statement to use:

[%if [@price@] > 200%]Free Shipping[%/if%]

Complex Statements

A complex logic statement contains two or more parameters. For example:

If you only wanted to display the "free shipping" text only when the price was greater than $200 and if the description was not empty you could use:

[%if [@price@] > 200 and [@description@] ne ''%]Free Shipping[%/if%]

Misc fields and Custom Configs

A common use case is utilising Custom Product Fields or globally accessible variables (Custom Configs, found within Settings & Tools) to display additional content or functionality based on criteria determined by these fields.

A key aspect to note is both Custom Product Fields and Custom Configs have the option to use Boolean operators (True,1), or (False, 0). The main difference between the options is that Custom Product Fields will output y (True) or n (False) based on the value specified whereas configs will produce 1 (True) or 0 (False).

Correct

[%if [@misc1@] eq 'y'%] <!-- Display featured content --> [%/if%]

It's very important to realise that both y and n are both True values in the context of traditional boolean operators.

Incorrect

[%if [@misc1@] %] <!-- Display featured content --> [%/if%]

If the control panel value is set to true or false, the content will still display as the logic you have presented as true either way because y and n are not false values.

This is not an issue for Custom Configs as they will output the typical Boolean values:

[%if [@config:display_special_content@] %] <!-- Display featured content --> [%/if%]
[%if [@config:display_special_content@] eq '1' %] <!-- Display featured content --> [%/if%]

Operators

== Equal to (for integers)
eq Equal to (for strings)
!= Not equal (for integers)
ne Not equal to (for strings)
< Less than
> More than
<= Less than or equal to
>= More than or equal to
or Either this or that
and Must be this and that
like Exists within string. Example: [%if "This is a string" like "is" %] would return true because is exists within the data provided.
not like Inverse of the like operator.

Was this article useful?

Be notified when this page is updated. Optional.