Library of Liquid Snippets

Cast.app uses Liquid to personalize visualizations and narrations. Here is a library of Liquid snippets you can use in your projects.

Contents

Handling Null Values in Variables

Use the default filter to set a default value to a variable to catch nulls or invalid values.

{{ this-variable-may-be-null | default: "" }}


{% assign pn = private_notes | default: "" %}
{% if pn == "INAPPDEMO" %}
	Setting up cast.app presentations to show in your app is easy.
{% endif %}

Convert a variable of type string to type number

Multiplying a variable by 1 using the times filter will convert a variable to a number.

{{ count-of-CSMs | times: 1 }}

Customer Since Snippet

Use the Customer Since snippet to return how long someone has been a customer with your company. This snippet is best used in the narration of your Welcome slide to personalize the greeting and thank them for being a customer.

You will pass in a date value using a Cast field to the dateStart variable in the snippet.

{% comment %} Snippet Name: CustomerSince {% endcomment %}
{% comment %} Input date customer started in dateStart {% endcomment %}
{% comment %} Output: 1 year and 6 days {% endcomment %}

{% comment %} For "2022/01/12" Output: 1 year and 6 days {% endcomment %}
{% comment %} For "2024/1/1" Output: 348 days in the future {% endcomment %}
{% comment %} For "2023/1/1" Output: 2 weeks and 3 days {% endcomment %}
{% comment %} For "2022/7/11" Output: 6 months and 1 week {% endcomment %}
{% comment %} For "2021/4/17" Output: 1 year and 9 months {% endcomment %}



{% assign dateStart = "2021/4/17" %}

{% assign dateStart = dateStart | date: '%s'%}
{% if dateStart != '-2208988800' and dateStart !='' and dateStart != null%}

    {% assign nowTimestamp = 'now' | date: '%s' %}

    {% comment %} difference in seconds {% endcomment %}
    {% assign diffSeconds = nowTimestamp | minus: dateStart %}

    {% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

    {% comment %} years, months, weeks, and days calculation {% endcomment %}
    {% if diffDays >= 365 %}
        {% assign years = diffDays|divided_by:365%}
        {% assign yearsRemainder = diffDays| modulo:365%}

        {% if years >1 %}
            {% capture years %}{{years}} years{% endcapture %}
        {% else %}
            {% capture years %}{{years}} year{% endcapture %}
        {% endif %}

        {% assign timeFrame = years %}

        {% if yearsRemainder >= 30 %}
            {% assign months = yearsRemainder|divided_by:30%}
            {% assign monthsRemainder = yearsRemainder| modulo:30%}

            {% if months >1 %}
                {% capture months %}{{months}} months{% endcapture %}
            {% else %}
                {% capture months %}{{months}} month{% endcapture %}
            {% endif %}

            {% capture timeFrame%}{{years}} and {{months}}{% endcapture %}


        {% elsif yearsRemainder >=7 and yearsRemainder < 30 %}

            {% assign weeks = yearsRemainder|divided_by:7 %}
            {% assign weeksRemainder = yearsRemainder|modulo:7 %}

            {% if weeks >1 %}
                {% capture weeks %}{{weeks}} weeks{% endcapture %}
            {% else %}
                {% capture weeks %}{{weeks}} week{% endcapture %}
            {% endif %}

            {% capture timeFrame %}{{years}} and {{weeks}}{% endcapture %}


        {% elsif yearsRemainder > 0 and yearsRemainder <7 %}

            {% assign days = yearsRemainder %}

            {% if days >1 %}
                {% capture days %}{{days}} days{% endcapture %}
            {% else %}
                {% capture days %}{{days}} day{% endcapture %}
            {% endif %}

            {% capture timeFrame %}{{years}} and {{days}}{% endcapture %}

        {% endif %}

    {% endif %}

    {% comment %} months, weeks, and days calculation {% endcomment %}
    {% if diffDays >=30 and diffDays < 365 %}

        {% assign months = diffDays|divided_by:30%}
        {% assign monthsRemainder = diffDays| modulo:30%}

        {% if months >1 %}
            {% capture months %}{{months}} months{% endcapture %}
        {% else %}
            {% capture months %}{{months}} month{% endcapture %}
        {% endif %}

        {% assign timeFrame = months %}

        {% if monthsRemainder >= 7 %}
            {% assign weeks = monthsRemainder|divided_by:7 %}
            {% assign weeksRemainder = monthsRemainder|modulo:7 %}

            {% if weeks >1 %}
                {% capture weeks %}{{weeks}} weeks{% endcapture %}
            {% else %}
                {% capture weeks %}{{weeks}} week{% endcapture %}
            {% endif %}

            {% capture timeFrame %}{{months}} and {{weeks}}{% endcapture %}

        {% elsif monthsRemainder > 0 and monthsRemainder < 7 %}

            {% assign days = monthsRemainder %}

                {% if days >1 %}
                    {% capture days %}{{days}} days{% endcapture %}
                {% else %}
                    {% capture days %}{{days}} day{% endcapture %}
                {% endif %}

                {% capture timeFrame %}{{months}} and {{days}}{% endcapture %}

        {% endif %}

    {%endif%}

    {% comment %} weeks and days calculation {% endcomment %}
    {% if diffDays >=7 and diffDays < 30 %}

        {% assign weeks = diffDays|divided_by:7 %}
        {% assign weeksRemainder = diffDays|modulo:7 %}

        {% if weeks >1 %}
            {% capture weeks %}{{weeks}} weeks{% endcapture %}
        {% else %}
            {% capture weeks %}{{weeks}} week{% endcapture %}
        {% endif %}

        {% assign timeFrame = weeks %}

        {% if weeksRemainder > 0 %}
            {% assign days = weeksRemainder %}

            {% if days >1 %}
                {% capture days %}{{days}} days{% endcapture %}
            {% else %}
                {% capture days %}{{days}} day{% endcapture %}
            {% endif %}

            {% capture timeFrame %}{{weeks}} and {{days}}{% endcapture %}
        {% endif %}

    {%endif%}

    {% comment %} days calculation {% endcomment %}
    {% if diffDays >=0 and diffDays < 7 %}

        {% assign days = diffDays %}

        {% if days >1 or days ==0 %}
            {% capture days %}{{days}} days{% endcapture %}
        {% else %}
            {% capture days %}{{days}} day{% endcapture %}
        {% endif %}

        {% assign timeFrame = days%}
    {%endif%}

    {% comment %} in future? {% endcomment %}
    {% if diffDays < 0 %}

        {% assign days = diffDays | abs %}

        {% if days >1 or days ==0 %}
            {% capture days %}{{days}} days in the future{% endcapture %}
        {% else %}
            {% capture days %}{{days}} day in the future{% endcapture %}
        {% endif %}

        {% assign timeFrame = days%}
    {%endif%}

{% else %}

{% assign timeFrame ='Invalid Date Value'%}

{% endif%}

{{timeFrame}}

Using the CustomerSince snippet in a narration.

Personalizing Email Subject

Use the Personalized Email Subject snippet to create a personalized email subject when you send a Cast email. A personalized email subject makes your emails more likely to be opened by your customers because it contains a specific metric or topic the individual would be interested in.

This snippet will display one of the 3 usage metrics from Cast fields (ActiveLearnersTotal, LearningHoursTotal, CourseCompletedTotal) in the email subject based on the criteria of how long the contact has been a customer. Copy and modify the snippet below using your own fields and criteria.

You would enter the snippet name in the Email Subject text box in the Compose Email section of your campaign.


{% comment %}
	Snippet Name:  EmailSubject
    Replace fields customerstartdate, ActiveLearnersTotal, LearningHoursTotal, and CoursesCompletedTotal with your own fields.
{% endcomment %}

{% assign customerstartdateinsecs = customerstartdate | default:"1970-01-01 00:00:00 UTC"|date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}
{% assign ActiveLearnersTotal = ActiveLearnersTotal|default:0%}
{% assign LearningHoursTotal = LearningHoursTotal|default:0%}
{% assign CoursesCompletedTotal = LearningHoursTotal|default:0%}


{% comment %} Difference in seconds {% endcomment %}
{% assign diffSeconds = nowTimestamp | minus: customerstartdateinsecs %}

{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

{% if diffDays <= 60%}

    {{contact_name}}, {{ActiveLearnersTotal}} active learner
    {%- if ActiveLearnersTotal != "1" %}s {% endif -%}
    spent {{LearningHoursTotal}} hour
    {%- if LearningHoursTotal != "1" %}s {% endif -%}
    learning in the last 30 days

{%elsif diffDays > 273%}

    {% comment %} Greater than 9 months {% endcomment %}

    {{contact_name}}, {{ActiveLearnersTotal}} active learner
    {%- if ActiveLearnersTotal != "1" %}s {% endif -%}
    completed {{CoursesCompletedTotal}} course
    {%- if CoursesCompletedTotal != "1" %}s {% endif -%}
    in the last 30 days

{%else%}

    {% comment %} Between 60 and 9 months {% endcomment %}
    {{contact_name}}, active learners spent {{LearningHoursTotal}} hour
    {%- if LearningHoursTotal != "1" %}s {% endif -%}
    learning in the last 30 days

{% endif %}

Personalizing Email Body

Use the Personalized Email Body snippet to provide personalized content in your Cast email for each contact. Personalized content will contain targeted topics that your cusotmer will be interested in learning about that would make them more likely to play the cast.

This snippet will return a salutation and one of the 3 usage metrics from Cast fields (ActiveLearnersTotal, LearningHoursTotal, CourseCompletedTotal) based on the criteria of how long the contact has been a customer. Copy and modify the snippet below using your own fields and criteria.

You would enter the snippet name in the Email Body text box in the Compose Email section of your campaign.


{% comment %}
	Snippet Name:  EmailBody
    Replace fields customerstartdate, ActiveLearnersTotal, LearningHoursTotal, and CoursesCompletedTotal with your own fields.
{% endcomment %}

{% assign customerstartdateinsecs = customerstartdate | default:"1970-01-01 00:00:00 UTC"|date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}

{% comment %} difference in seconds {% endcomment %}
{% assign diffSeconds = nowTimestamp | minus: customerstartdateinsecs %}

{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

We hope you're doing well!

{% assign CompletedCerts30DaysHighestTotal = CompletedCerts30DaysHighestTotal | default: 0 | times: 1 %}

{% if CompletedCerts30DaysHighestTotal > 0 %}
  In the last 30 days, {{ CompletedCerts30DaysHighestTotal | cast_highlight }} learner{% if CompletedCerts30DaysHighestTotalLabel != 1 -%}s{% endif %} passed the {{ CompletedCerts30DaysHighestTotalLabel | cast_pronounce | cast_highlight }}, which is {{ CompletedCerts30DaysHighestPercentage | cast_highlight }} of your total certified learners.
{% endif %}

Track your learners' progress, set goals, and identify areas for improvement.

{% if diffDays <= 60 %}
Get the most from your new ACME investment!
{% else %}
Continue to get the most out of your ACME investment!
{% endif %}


Using the EmailSubject and EmailBody snippets to personalize the Cast email sent to your contacts.

Check If String Exists in an Array

Use Case

Check if a Cast contact field is contained in a Cast array field to return a state

You can check if the value of a contact field exists in a Cast array field and return True or False.

The IsEMEA snippet checks if the contact field called country exists in the array field called EMEA_Countries.

{% comment %}
Snippet Name: IsEMEA
This snippet checks if the contact field called "country" and is found in the array field called EMEA_Countries.
{% endcomment %}

{% if EMEA_Countries contains country %}
  True
{% else %}
  False
{% endif %}

This snippet is used in Generate Conditions of a campaign to only generate casts if the contact is from an EMEA country.

The array field EMEA_Countries contains countries shown at the bottom of the screenshot.

The contact field β€œcountry” for Cheryl is the United States.

The IsEMEA snippet is used in Generate Conditions and Cheryl will not have a cast generated for her.

Benchmarking and Gamification using Liquid

Below is an example using Liquid to compare a field containing usage to a specific customer account to a field with a calculated average of all the customer accounts to do a benchmark comparison and create the narration for it. The field values are from the datasets provided by the customer.

{% assign u = accountUsage|times:1 %}
{% assign a=  avgUsage|times:1 %}
{% if u  > a %}
    {% assign percentage = u| divided_by:a|times:100|round:0 %}
    Your product usage within SaaS Magic is {{percentage}} above the industry average of companies similar to your size. Congratulations!
{% else %}
    Your product usage within SaaS Magic is below the industry average
    of companies similar to your size.

    Contact your {{"CSM" | cast_pronounce }} for ideas on
    how you can increase your usage in the platform.
{% endif %}

Output:

    Your product usage within SaaS Magic is below the industry average
    of companies similar to your size.

    Contact your CSM for ideas on
    how you can increase your usage in the platform.

Use Case #1

A customer wanted the narrative for their product usage to dynamically point out to the customer either a positve or negative outcome based on their current usage vs. their usage from the previous quarter. The snippet analysis_usage_determination was created to accomplish this. The Liquid code in the snippet compares the field that contains customers’ average from the previous quarter, analysis_prev_qtr_avg, to the field that contains the average in the current quarter, analysis_current_qtr_avg. The narration shown to the customer is based on whether their usage in the current quarter increased, decreased, or stayed the same. Liquid is used to calculate the percentage increase or decrease of usage to be shown in narration.

{% comment %}
Narration for Analyis scene. There are 3 branches: neutral, positive, and negative.
{% endcomment %}

{% assign analysis_current_qtr_avg = analysis_current_qtr_avg | times:1.0 %}
{% assign analysis_prev_qtr_avg = analysis_prev_qtr_avg | times:1.0 %}

{%- if analysis_current_qtr_avg > analysis_prev_qtr_avg -%}

Compared to the previous three month period, your monthly average increased by --up--{{analysis_current_qtr_avg|minus:analysis_prev_qtr_avg|divided_by:analysis_current_qtr_avg|times:100|ceil|abs}}%. This was awesome to see!

{%- elsif analysis_current_qtr_avg < analysis_prev_qtr_avg -%}

However, your average monthly usage decreased from the previous three month period by {{analysis_prev_qtr_avg|minus:analysis_current_qtr_avg|divided_by:analysis_prev_qtr_avg|times:100|ceil|abs}}%.

{%- elsif analysis_current_qtr_avg == analysis_prev_qtr_avg -%}

This usage level is consistent with the previous three month period as well!
{%- endif%}

The snippet is inserted in the narration box in a slide. The text the snippet generates is shown under the box. The example below is a customer whose usage increased compared to the previous quarter.

The example below is a customer whose usage decreased compared to the previous quarter. Notice in both examples that all you have to do is create the snippet and the narrative text shown to the customer will change based on their data.

Use Case #2

A customer wanted the product usage narration to point out to the customer the amount of storage they used and the percentage used relative to their total subscripton. The snippet LeastPercentageUsed0to10 calculates the percentage used.

{% comment %}
This snippet calculates the percentage used of the lowest usage accounts
{% endcomment %}

{%assign LicenseUsage0to10LeastUsed = LicenseUsage0to10Least|times:1%}
{%assign LicenseUsage0to10LeastReserve = LicenseUsage0to10LeastReserve|times:1%}

{%assign percentageUsed = LicenseUsage0to10LeastUsed|divided_by:LicenseUsage0to10LeastReserve|times:100%}

{{percentageUsed|round:2}}

You can insert Liquid code in the narration box as well as snippets to generate the text you want. Below is an example of using the snippet LeastPercentageUsed0to10 in the narration box as well as other Liquid code. if-endif block is used to check if the perentage that is calculated by the snippet is greater than 1, and if so use the round filter to display the percenatage with no decimals. The custom cast_footnote filter is used so that the contract name won’t be read by the bot, but it willl be displayed in the narration. The append filter is used to add the β€˜%’ and β€˜TiBs’ to the respective text.

The text that is generated from the snippet and Liquid is code shown under the box.

Here is the complete Liquid code used in the narration box.

{%assign LeastPercentageUsed0to10 = LeastPercentageUsed0to10|times:1%}

{% if LeastPercentageUsed0to10 >= 1%}
{%assign LeastPercentageUsed0to10 = LeastPercentageUsed0to10|round%}
{% endif %}

This site{{ LicenseUsage0to10LeastLabel | cast_footnote }} used only {{ LicenseUsage0to10Least | cast_highlight }} {{ "TiBs" | cast_pronounce|cast_highlight }} during this period, which is {{LeastPercentageUsed0to10|append:"%"| cast_highlight }} of the reserved capacity available.