About this article
This article explains what the Switch() function is and provide examples of how it can be used with the binding syntax in smart templates.
Prerequisites
|
What is the Switch() function?
Switch() is a function that compares one value to multiple values and returns results accordingly, if nothing matches the conditions set in the function, a default value will be inserted. It can also be seen as slightly similar to a nested IfElse() function.
Switch() logic
Syntax | {{Switch(ValueToCompare, Value1, Result1, Value2, Result2..., DefaultValue)}} |
Input | ValueToCompare, Value1, Value2..., DefaultValue |
Output | Value1 -> Result1, Value2 -> Result2, Else -> DefaultValue |
Switch() function examples
Example 1
Using the Switch() function with the "Sensitivity" dropdown question originating from the response form to adjust the text displayed in the template.
Binding | {{Switch(Form.Sensitivity.Name,"Top Secret","Strictly Confidential","Internal","Internal Only","Public")}} | ||
Input | Form.Sensitivity = "Top Secret" | Form.Sensitivity = "Internal" | Form.Sensitivity = Not "Internal" or "Top Secret" |
Output | "Strictly Confidential" | "Internal Only" | "Public" |
Example 2
Using the Switch() function to show the correct salutation depending on the HostSystem value of "Gender".
Binding | {{Switch(HostSystem.Gender, "M", "Dear sir", "F", "Dear madam", "Dear sir/madam")}} | ||
Input | HostSystem.Gender = "M" | HostSystem.Gender = "F" | HostSystem.Gender = "" |
Output | "Dear sir" | "Dear madam" | "Dear sir/madam" |
Example 3
Using the Switch() function with the "Company" originating from the user profile to translate a numerical value into a company name.
Binding | {{Switch(UserProfile.Company,"1","Templafy ApS","2","iWriter","No Company")}} | ||
Input | UserProfile.Company = "1" | UserProfile.Company = "2" | UserProfile.Company = Not "1" or "2" |
Output | "Templafy ApS" | "iWriter" | "No Company" |
Example 4
Instead of plain text you can also use other form fields in the binding, e.g. Form.Subject.
The value of Form.Subject is "Meeting".
Binding | {{Switch(Form.Choice,"1", "Hello world", "2", Form.Subject, "No Choice")}} | ||
Input | Form.Choice = "1" | Form.Choice = "2" | Form.Choice = Not "1" or "2" |
Output | "Hello world" | "Meeting" | "No Choice" |
Example 5
Using the Switch() function together with the StringJoin() function to display specific text depending if both questions 1 and 2 are empty.
Binding | {{Switch(StringJoin("", Form.Question1, Form.Question2),"", "Question 1 and 2 are empty", "Question 1 and/or 2 contain(s) a value")}} | |
Input |
Form.Question1 = "" Form.Question2 = "" |
Form.Question1 = "" Form.Question2 = "Hello world" |
Output | "Question 1 and 2 are empty" | "Question 1 and/or 2 contain(s) a value" |
Example 6
In this example this Offices data source is used:
Using the Switch() function together with the "DocumentLanguage" originating from the user profile to display the phone number in different formats.
Depending on the DocumentLanguage set in the user profile the phone number is shown in a different format. If the DocumentLanguage is not "nl-NL" or not set then the default value is returned.
Binding | {{Switch(DocumentLanguage,"nl-NL", UserProfile.Office.Phone_NL, UserProfile.Office.Phone_EN)}} | ||
Input |
DocumentLanguage = "nl-NL" |
DocumentLanguage = "en-US" | DocumentLanguage = not set |
Output | "040 - 1234 567" | "+31 (0)40 1234 567" | "+31 (0)40 1234 567" |
Related articles
Comments
Article is closed for comments.