Articles in this section

How to provide an output based on a condition using IfElse()

About this article

This article explains what the IfElse() function is and provide examples of how it can be used with the binding syntax in smart templates.  

 

Prerequisites

 

 

What is the IfElse() function? 

IfElse() is a commonly used function that outputs values based on a condition. The function is commonly used with smart fields (with the binding syntax or the visibility), and can also be used with template settings and properties.

 

IfElse() logic

Syntax {{IfElse(Condition, TrueValue, FalseValue)}}
Input Condition
Output TrueValue/FalseValue

 

 
  • Rather than nesting multiple IfElse() statements for multiple comparisons, we recommend Switch() or And() and Or() functions instead.

 

IfElse() function examples

 

Example 1

Using the IfElse() function to set the visibility of a smart field based on the value of the 'Subject' question originating from the response form.

Binding {{IfElse(Equals(Form.Subject, "Test"), VisibilityType.Hidden, VisibilityType.Visible)}}
Input Form.Subject = "Test" Form.Subject = "Something else"
Condition True False
Output Hidden text Visible text

 

Example 2

Using the IfElse() function together with FormatDateTime() to set the visibility of a smart field based on the value of the 'Date' question (of type Date) originating from the response form.

Binding {{IfElse(Equals(FormatDateTime(Form.Date,"d-M-yyyy"), ""), VisibilityType.Hidden, VisibilityType.Visible)}}
Input Form.Date = empty Form.Date = filled
Condition True False
Output Hidden text Visible text

 

 
  • Because Equals() can only compare fields of type text (String), you have to add FormatDateTime() to your function to set visibility based on a form field of type Date.
  • In the example the format "d-M-yyyy" is used, but this can be any format.

 

Example 3

Using the IfElse() function together with FormatNumber() to set the visibility of a smart field based on the value of the 'Number' question (of type Number) originating from the response form.

Binding {{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), VisibilityType.Hidden, VisibilityType.Visible)}}
Input Form.Number = "1" Form.Number = "123"
Condition True False
Output Hidden text Visible text

 

 
  • Because Equals() can only compare fields of type text (String), you have to add FormatNumber() to your function to set visibility based on a form field of type Number.
  • Use this binding in the Visibility Expression field to configure a Custom visibility.
  • Use the format '#'.

 

Example 4

Using the IfElse() function together with FormatNumber() to set the visibility of a smart field based on the value of the 'Number' question (of type Number) originating from the response form.

Binding {{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), "Hello world", "The quick brown fox jumps over the lazy dog")}}
Input Form.Number = "1" Form.Number = "123"
Condition True False
Output "Hello world" "The quick brown fox jumps over the lazy dog"

 

 
  • Because Equals() can only compare fields of type text (String), you have to add FormatNumber() to your function to set visibility based on a form field of type Number.
  • Use this binding in the Binding field.
  • Use the format '#'.

 

Example 5

Using the IfElse() function together with Contains() to inject specific product information in the smart template based on the 'Subject' question (of type Text) originating from the response form.

Binding {{IfElse(Contains(Form.Subject, "Product A"), "Product A is $100", "Pricing list available upon request")}}
Input Form.Subject = "We are showcasing Product A" Form.Subject = "We are showcasing different products"
Condition True False
Output "Product A is $100" "Pricing list available upon request"

 

Example 6

Using the IfElse() function to show the correct salutation depending on the HostSystem value of 'Gender'.

Binding {{IfElse(Equals(HostSystem.Gender, "M"), "Dear sir", IfElse(Equals(HostSystem.Gender, "F"), "Dear madam", "Dear sir/madam"))}}
Input HostSystem.Gender = "M" HostSystem.Gender = "F" HostSystem.Gender = ""
Output "Dear sir" "Dear madam" "Dear sir/madam"

 

 
  • Instead of using a nested IfElse() binding as shown above, you can also use the Switch() function in this case (see example 2 in that article).

 

Example 7

Using the IfElse() function together with Not() and Equals() to inject specific product information in the smart template based on the 'Subject' question (of type Text) originating from the response form.

Binding  {{IfElse(Not(Equals(Form.Subject, "Product A")), "Description of all other products", "Description of product A")}}
Input

Form.Subject = "As you can see our products are compliant"

Form.Subject = "Product A"

Condition True False
Output "Description of all other products" "Description of product A"

 

Example 8

Using the IfElse() function to set the visibility of a smart field based on the value of the 'CheckboxInclude' checkbox question (of type Checkbox) originating from the response form

Binding {{IfElse(Form.CheckboxInclude, VisibilityType.Visible, VisibilityType.Hidden)}}
Input Form.CheckboxInclude = "☑" Form.CheckboxInclude = "☐"
Condition True False
Output Visible text  Hidden text

 

Example 9

Using the IfElse() function together with the And() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' and 'Product C' must both be checked for the text to be visible.

Binding {{IfElse(And(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden)}}
Input

Form.ProductA = "☑"

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductC = "☑"

Condition True False
Output Visible text  Hidden text

 

Example 10

Using the IfElse() function together with the Or() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' or 'Product C' must be checked for the text to be visible.

Binding {{IfElse(Or(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}
Input

Form.ProductA = ""

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductC = "☐"

Condition True False
Output Visible text  Hidden text

 

Example 11

Using the IfElse() function together with And() and Not() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' and 'Product C' must both be checked, and 'Product B' must be unchecked for the text to be visible.

Binding {{IfElse(And(Form.ProductA, Not(Form.ProductB), Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}
Input

Form.ProductA = "☑"

Form.ProductB = "☐"

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductB = "☑"

Form.ProductC = "☑"

Condition True False
Output Visible text Hidden text

 

Example 12

Using the IfElse() function together with Contains() to display or hide some text based on the value of the 'UserProfile.Office.Name' originating from the user profile.

  • If the 'UserProfile.Office.Name' is 'Copenhagen' or 'Berlin', the text should be hidden.
  • If the 'UserProfile.Office.Name' is anything else, the text should be shown.
Binding {{IfElse(Contains("Copenhagen, Berlin", UserProfile.Office.Name), VisibilityType.Hidden, VisibilityType.Visible)}}
Input UserProfile.Office.Name = "Berlin" UserProfile.Office.Name = "Copenhagen" UserProfile.Office.Name = "Eindhoven"
Output Hidden text Hidden text Visible text

 

Example 13

Using the IfElse() function to dynamically insert a text element/slide based on the value of the 'Checkbox' checkbox question (of type Checkbox) originating from the response form.
The value '123456787654321' used in the example is the Asset Id of the text element.

Binding {{IfElse(Form.Checkbox,"123456787654321","")}}
Input Form.Checkbox = "☑" Form.Checkbox = "☐"
Condition True False
Output Text element/slide inserted  Text element/slide not inserted 

 

 

 

Example 14

Using the IfElse() function together with Equals() to display the value of the 'Subject' question (of type Text) originating from the response form if the HostSystem value is empty.

Binding {{IfElse(Equals(HostSystem.Subject, ""), Form.Subject, HostSystem.Subject)}}
Input HostSystem.Subject = "",
Form.Subject = "Hello world"
HostSystem.Subject = "Invitation",
Form.Subject = "Hello world"
Condition True False
Output "Hello world" "Invitation"

 

Related articles

 

 

classification chart title tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.