Conditional Functions

In this article:

if/else Function

The #if helper function is used to verify whether a value of a user or event attribute exists or is true. Upon message dispatch, if the verified attribute's value is equal to false, undefined, null, "", 0, or [], ActivatePro will not render the block.

The syntax of this helper function is:

{{#if [attribute]}}
   ...
{{/if}}
Property Description
attribute The user or event attribute that is used for the verification. e.g. user.[traits.birthday], [properties.price]

Use Cases

Display content only if it exists

For this example, we only display the user's first name if it exists.

{{#if user.[traits.firstName]}}
    <h1>Hi {{user.[traits.firstName]}}!</h1>
{{else}}
    <h1>Hi there!</h1>
{{/if}}

Once the function is evaluated, it displays either:

<!--Assuming the user's first name would be John.-->
<h1>Hi John!</h1>

or

<!--Whenever the first name attribute does not exist for a user.-->
<h1>Hi there!</h1>

cif/else Function

ActivatePro offers a more advanced version of the #if helper function that allows you to perform additional operations and only display content if a certain criteria is met.

The syntax of this helper function is:

{{#cif '{
    "property": "attribute", 
    "operator": "operator", 
    "value": "value"
}'}}
    ...
{{else}}
    ...
{{/cif}}
For a list of operators that can be used for comparison, please check the Operators page.
 
Optionally, you may use the {{else}} block that will display other content if the condition is not met. The {{else}} block should be placed within the {{#cif}}{{/cif}} tags for the function to be correctly processed.
Property Description
attribute

The user or event attribute that is used for the verification. e.g. user.[traits.birthday], [properties.price]

Please note that journey steps helper with array attribute e.g. cart is not supported for advanced If.

operator The operator that will be used to asses whether the criteria is met or not. e.g. ==. <=. See the list of all available operators and their compatibility below.
value The value the attribute property will be compared against. e.g male, 0

Use Cases

Gender Based Salutation

For this example, we output the salutation of a client according to their gender.

Dear {{#cif '{
   "property": "user.[traits.gender]", 
   "operator": "==", 
   "value": "male"
}'}}
   Mr. {{user.[traits.lastName]}}
{{else}}
   {{#cif '{
       "property": "user.[traits.gender]", 
       "operator": "==", 
       "value": "female"
   }'}}
       Mrs. {{user.[traits.lastName]}}
   {{else}}
       Visitor
   {{/cif}}
{{/cif}}

Once the function is evaluated, it displays the salutation according to the user's gender. For example, the function may result in:

  • Dear Mr. Doe, if the user is male
  • Dear Mrs. Doe, if the user is female

  • Dear Visitor, if the user's gender is unknown

     

    Stock Level Low

    For this example, we output a call to action statement when the stock of a certain product is low.

    {{#cif '{
        "property": "[properties.stockQuantity]", 
        "operator": "<", 
        "value": "4"
    }'}}
        <p>Less than 4 items available. Buy now!</p>
    {{/cif}} 

    Once the function is evaluated, if a product's stock is less than 4, it displays the content below in the section where it is used in the message.:

    <p>Less than 4 items available. Buy now!</p>

    Newsletter Subscription 

    We can double check in the email address is available as a trait and say thank you for subscription:

    {#cif '{"property":"user.[traits.email]","operator":"any"}'}}Thank you for Newsletter subscription{{/cif}}
     

    Fetch event data with cif condition

    In our platform, it is possible to retrieve specific data from journey step events, which is especially useful when working with real-time campaigns. For instance, consider a scenario in which we have configured a real-time campaign that is initiated by the event Completed Order.

    Within the sequence of journey step events, the event that triggers the campaign will always be indexed as [0], since it represents the initial point of interaction. This index can be referenced to access and utilize various properties embedded within the triggering event.

    To extract a specific property—such as the gender attribute from the Completed Order event—we can use a Handlebars expression. The corresponding handlebar syntax to fetch the value of the properties.gender field from the first journey step event is as follows:

    {{#cif '{
    
    "property": "journey.steps.[0].[properties.gender]", 
    
    "operator": "==",
    
    "value": "male"
    
    }'}}
    
    Mr. {{user.[traits.lastName]}}
    
    {{else}}
    
    {{#cif '{
    
    "property": "journey.steps.[0].[properties.gender]",
    
    "operator": "==",
    
    "value": "female"
    
    }'}}
    
    Mrs. {{user.[traits.lastName]}}
    
    {{else}}
    
    Visitor
    
    {{/cif}} 
    {{/cif}} 
    {{/cif}} 

    Fetch 2 conditions within journey steps

    In order to verify whether both conditions have been fulfilled within the context of the journey steps, we can utilize the following Handlebars expression.

    {{#cif '{
    
        "property": "journey.steps.[0].[properties.Low_Budget_Rate]",
    
        "operator": ">",
    
        "value": "0"
    
    }'}}
    
    {{#cif '{
    
        "property": "journey.steps.[0].[properties.High_Budget_Rate]",
    
        "operator": ">",
    
        "value": "0"
    
    }'}}
    
        {{journey.steps.[0].[properties.LowBudgetRateValue]}} - {{journey.steps.[0].[properties.HighBudgetRateValue]}} {{journey.steps.[0].[properties.Currency]}}
    
     {{/cif}}
    
    {{/cif}}

    Explanation:

    • The first {{#cif}} block checks whether the Low_Budget_Rate in the first journey step is greater than zero.
    • The second (nested) {{#cif}} block verifies that the High_Budget_Rate is also greater than zero.
    • Only if both conditions are met, the output will render the Low_Budget_Rate, High_Budget_Rate, and the associated Currency.