top of page
Writer's pictureNaveen VM

Guided Template Language (GTL) in Salesforce Marketing Cloud (Part 2)

This is the Second part of GTL. Please visit here for Part 1 of GTL article which contains Basics of GTL, GTL Syntax, For Loop, If condition & parsing nested JSON in GTL.


In this article, let's talk about Switch conditions in GTL, different ways to use ampscript in GTL, passing values from GTL to SSJS & using GTL in cloud pages.


Just for reference, for all the below GTL use cases we will be using this sample JSON which will be present in Order data field in a data extension:

Switch in GTL:


Switch in GTL is nothing but a case statement which checks each condition mentioned in switch and it is indicated with {{#switch}}. This block checks if the attribute is matching the value in switch statement (refer to sample data section above for test data).

What does the above code do?

line 18 - start with switch statement

line 19 - checks if Number_of_Ratings = 15 if yes then it will print 'Rating 15'

Line 20 - checks if Number_of_Ratings = 3 if yes then it will print 'Rating 3'


Output:

In the below screenshot, you can see for rating 3 & 15 it got printed for rating 14 it is blank.

Switch Statement in GTL

Passing values from GTL to Ampscript:

For this case, I will use the same IF condition scenario as in the previous blog (Part 1).


In the below scenario, the use case is that we shouldn't show the product name if the SKU = '104176' (refer to sample data section above for test data).


What does the above code do?

Line 18 - We use TreatAsContent function in ampscript to fetch the GTL values and pass it to ampscript.

Line 19 - Using regular IF statement in ampscript evaluating the criteria.


Output:

In the below screenshot, you can see that product name - 'Crimson Camelback Pack' is not displayed because of the SKU = 104176.

Passing Values from GTL to Ampscript

Ampscript functions inside GTL:

You can also use some small ampscript functions inside GTL.

Below example shows using concat function in ampscript combining SKU & Product name in a single line (refer to sample data section above for test data).

What does the above code do?

Line 17 - Using Concat ampscript function combining both SKU & Product Name GTL values.


Output:

In the below screenshot Concat SKU + product will have the values.

Ampscript inside GTL

Passing values from GTL to SSJS:

Using the TreatAsContent SSJS platform library function, you can pass the values from GTL to SSJS.

The use case here is to display the value of SKU from GTL to SSJS (refer to sample data section above for test data).

What does the above code do?

Line 19 - Fetching the value from GTL to SSJS platform library using TreatAsContent

Line 21 - Displaying the SKU value using SSJS for the variable SKU


Output:

If you look at the below screen shot the SKU is getting displayed from SSJS.

Passing values from GTL to SSJS

GTL in Cloud Page:

All the above examples for GTL Part 1 & 2 will be for email.

But if you are planning to use GTL in cloud page then add this "%%{={{ }}=}%%" before you start GTL. Credit goes to Eilot from this stack exchange post.


Line 5 - resets the GTL parameters.


Output:

GTL in Cloud Pages

This is end of GTL series. I hope you can get some insights on how GTL works!


Additional Reads:


Please feel free to let me know if you find this article useful and leave your feedback/comment in case of any queries.


Happy Learning!


2,335 views0 comments

Comments


bottom of page