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 |
TipInstead of nesting multiple IfElse() statements for multiple comparisons, we recommend Switch() or And() and Or() functions. |
IfElse() function examples
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. The binding is:
{{IfElse(Equals(Form.Subject, "Test"), VisibilityType.Hidden, VisibilityType.Visible)}}
Example A | Example B | |
---|---|---|
Input | Form.Subject = "Test" | Form.Subject = "Something else" |
Condition | True | False |
Output | Hidden text | Visible text |
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. The binding is:
{{IfElse(Equals(FormatDateTime(Form.Date,"d-M-yyyy"), ""), VisibilityType.Hidden, VisibilityType.Visible)}}
Example A | Example B | |
---|---|---|
Input | Form.Date = empty | Form.Date = filled |
Condition | True | False |
Output | Hidden text | Visible text |
Note
|
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. The binding is:
{{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), VisibilityType.Hidden, VisibilityType.Visible)}}
Example A | Example B | |
---|---|---|
Input | Form.Number = "1" | Form.Number = "123" |
Condition | True | False |
Output | Hidden text | Visible text |
Note
|
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. The binding is:
{{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), "Hello world", "The quick brown fox jumps over the lazy dog")}}
Example A | Example B | |
---|---|---|
Input | Form.Number = "1" | Form.Number = "123" |
Condition | True | False |
Output | "Hello world" | "The quick brown fox jumps over the lazy dog" |
Note
|
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. The binding is:
{{IfElse(Contains(Form.Subject, "Product A"), "Product A is $100", "Pricing list available upon request")}}
Example A | Example B | |
---|---|---|
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" |
Using the IfElse() function to show the correct salutation depending on the HostSystem value of 'Gender'.
The binding is:
{{IfElse(Equals(HostSystem.Gender, "M"), "Dear sir", IfElse(Equals(HostSystem.Gender, "F"), "Dear madam", "Dear sir/madam"))}}
Example A | Example B | Example C | |
---|---|---|---|
Input | HostSystem.Gender = "M" | HostSystem.Gender = "F" | HostSystem.Gender = "" |
Condition | True | False>True | False>False |
Output | "Dear sir" | "Dear madam" | "Dear sir/madam" |
NoteInstead 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). |
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. The binding is:
{{IfElse(Not(Equals(Form.Subject, "Product A")), "Description of all other products", "Description of product A")}}
Example A | Example B | |
---|---|---|
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" |
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. The binding is:
{{IfElse(Form.CheckboxInclude, VisibilityType.Visible, VisibilityType.Hidden)}}
Example A | Example B | |
---|---|---|
Input | Form.CheckboxInclude = "☑" | Form.CheckboxInclude = "☐" |
Condition | True | False |
Output | Visible text | Hidden text |
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. The binding is:
{{IfElse(And(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden)}}
Example A | Example B | |
---|---|---|
Input |
Form.ProductA = "☑" Form.ProductC = "☑" |
Form.ProductA = "☐" Form.ProductC = "☑" |
Condition | True | False |
Output | Visible text | Hidden text |
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. The binding is:
{{IfElse(Or(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}
Example A | Example B | |
---|---|---|
Input |
Form.ProductA = "☐" Form.ProductC = "☑" |
Form.ProductA = "☐" Form.ProductC = "☐" |
Condition | True | False |
Output | Visible text | Hidden text |
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. The binding is:
{{IfElse(And(Form.ProductA, Not(Form.ProductB), Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}
Example A | Example B | |
---|---|---|
Input |
Form.ProductA = "☑" Form.ProductB = "☐" Form.ProductC = "☑" |
Form.ProductA = "☑" Form.ProductB = "☑" Form.ProductC = "☑" |
Condition | True | False |
Output | Visible text | Hidden text |
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.
The binding is:
{{IfElse(Contains("Copenhagen, Berlin", UserProfile.Office.Name), VisibilityType.Hidden, VisibilityType.Visible)}}
Example A | Example B | Example C | |
---|---|---|---|
Input | UserProfile.Office.Name = "Berlin" | UserProfile.Office.Name = "Copenhagen" | UserProfile.Office.Name = "Eindhoven" |
Condition | True | True | False |
Output | Hidden text | Hidden text | Visible text |
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. The binding is:
{{IfElse(Form.Checkbox,"123456787654321","")}}
Example A | Example B | |
---|---|---|
Input | Form.Checkbox = "☑" | Form.Checkbox = "☐" |
Condition | True | False |
Output | Text element/slide inserted | Text element/slide not inserted |
ImportantIf the text element/slide inserted contains questions in the response form, a dropdown question should be used, as the text element/slide questions will not be displayed when using a checkbox. |
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. The binding is:
{{IfElse(Equals(HostSystem.Subject, ""), Form.Subject, HostSystem.Subject)}}
Example A | Example B | |
---|---|---|
Input | HostSystem.Subject = "", Form.Subject = "Hello world" |
HostSystem.Subject = "Invitation", Form.Subject = "Hello world" |
Condition | True | False |
Output | "Hello world" | "Invitation" |
Comments
Article is closed for comments.