top of page

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

Updated: May 16, 2021

What is Guided Template Language (GTL)?

GTL is a one of template languages in SFMC which is based from Handlebars.js and Mustache.js template languages to send personalized & dynamic messages. Other email delivery products like Sendgrid also uses handlebars for their personalization.


When do you use GTL?

For me personally, I used GTL only to parse JSON and show the dynamic content in email/SMS/Push notifications which is what it is good at. It will be very difficult to parse JSON using ampscript, which is where GTL comes into play.


For example, GTL can be used in Transactional emails like reservation confirmation, order confirmation emails where 3rd party system will be passing the data in JSON format to SFMC either by Transactional API, Triggered sends or Journey builder API and in some cases you will get the whole JSON object data in string. In those scenarios you would need to parse the JSON and show the content in email.


GTL Syntax:

A GTL expression will start and ends with handlebars - "{{", some contents, followed by a "}}". When the template is executed, these expressions are replaced with values from an input object.

{{content}}

Sample Data:

Below is the same record details which we will leverage for GTL examples:

Sample data records for GTL
Sample data for GTL

Sample JSON in Order data field to be used in following below examples:

Simple GTL:

To retrieve the Firstname from the data extension record you can just simply use - {{Firstname}}

Output:

Simple GTL Syntax

For Loop in GTL:


For loop in GTL is indicated with 'Each' block. This block iterates specified nodes on rows and renders the respective inner mark up for each row (refer to sample data section above for test data).

What does the above code do?

Line 3 - Fetching the data from the data extension attribute called 'OrderData'.

Line 11 - You would need either 'Dataobjects' or 'Datasources' to send the information to GTL. When you declare Dataobjects or Datasources, you would also need to specify a name. In this case, it's called 'JsonVar'.

There are 5 types of Datasource / Dataobject :

  • query

  • inline

  • list

  • variable

  • nested

In this case, we are using variable to retrieve the array of JSON.

Line 16: each block is a for loop, in this case it will iterate nodes and looking for specific element called 'Product_Name' and it will return the output.

Output:

GTL with For Loop

If Condition in GTL:

If condition in GTL is indicated with {{#if}} block. It can check if a specific element is present in JSON and modify content / data based upon it.

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 17 - In the for loop, we are checking the SKU element should not contain '104176'. So that specific product name will not show in email.


Output:

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

GTL with If Condition

Nested JSON in GTL:

In this scenario, we will need to retrieve values for the element - 'Number_of_Ratings' which is in nested JSON.

So, in this case we need to use of Dataobject / Datasource of type as variable and then we need to use of type as nested to get the respective element (refer to sample data section above for test data).

What does the above code do?

Line 19 - We are using datasource to retrieve the information with the type as nested to go into the nested elements.

Line 21 -To get the 'Number_of_Ratings', we need to find the nested node from JSON which is 'Product_Ratings'. So that will be the target.

Line 23 - Is to fetch 'Number_of_Ratings'.

Outcome:

You can see from the below screenshot, we have retrieved the number of ratings from nested JSON.

GTL With Nested JSON

This is just a Part 1 of GTL and I hope you can get some insights on how GTL works!

Check out for GTL Part 2 here.!


Additional reads:

What the hell,GTL - By Greg Gifford (gortonington)


Please feel free to leave your feedback/comment in case of any queries.


Happy Learning!


6,049 views3 comments

Recent Posts

See All
bottom of page