GTL is limited in SFMC. I'd stick with AMPScript.
You can do what you're wanting with AMPScript in an email (triggered or otherwise):
%%[
var @firstName, @link
set @firstName = AttributeValue("firstName")
set @link = AttributeValue("link")
]%%
Hi%%=iif(not empty(@firstName),concat(", ",@firstName),"")=%%.
<br><br>Please, click this <a href="%%=redirectto(@link)=%%" alias="link">link</a>.
The firstName
and link
attributes don't have to have matching Profile Attributes or a columns in a Triggered Send templated Data Extension (it is a good practice, however). As long as you are passing them in the payload of your API call, you'll have access to them in the email.
There are a few more details in my answer on this question:
How do I pass data to an email via a triggered send?
I ran into the same problem. GTL will only insert a link from JSON data into an href if link tracking is disabled. I also looked for a way to let GTL parse the value out of my JSON data and then pass it into AMPscript to workaround the href issue.
It is possible to read the GTL value into AMPscript via the TreatAsContent function. With the GTL tag inside of the TreatAsContent function then it will be correctly dereferenced within the scope of the current data context, and the value gets returned to the caller of the AMPscript function. Thus you can put together an href that follows a link from a GTL tag like so:
<a href="%%=TreatAsContent('{{url}}')=%%">click me</a>
And if you want to explicitly enable link tracking (considered a good practice by SFMC), you can chain the TreatAsContent function with the RedirectTo function like so:
<a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">click me</a>
If you prefer not to inline the AMPscript call or if you need the json value as a variable, you can use TreatAsContent to set an AMPscript variable with the GTL value:
%%[ var @url set @url = TreatAsContent("{{url}}") ]%%
<a href="%%=v(@url)=%%">click me</a>
Similarly, you can do the same thing in SSJS if you need the GTL value there:
<script runat="server">
Platform.Load("core","1");
var url = Platform.Function.TreatAsContent("{{url}}");
</script>
And here is a sendable example showing what it looks like when you put this together with the GTL datasource:
<html>
%%[
var @Json set @Json =
'{"url":"http://www.google.com/?query=42","thumb":"http://www.wpclipart.com/small_icons/buttons/.cache/button_green.png"}'
]%%
<body>
{{.datasource JSONVar type=variable}}
{{.data}}
{ "target" : "@Json" }
{{/data}}
Click the button to follow the link: <a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%"><img alt="{{name}}" src="%%=TreatAsContent('{{thumb}}')=%%" height="100" width="100"/></a>
{{/datasource}}
</body>
</html>
Best Answer
Guide Template Language is able to use AMPscript functions using simple tags and the = prefix. Guide will pass any parameters included in the inner text of the template to the specified function. Once completed, Guide replaces the template with the output of the function.
Currently Guide does not support user created functions like Handlebars.js does natively.
List of AMPscript functions that should be available within the Guide and follows the same parameters for usage.
Basic structure:
Example:
Displays the result of
2
.You must quote string and date literals. You can optionally quote numeric literals. You can include an ampersand (
&
) character or enclose the variable with single curly brackets ({}
) to render the content as un-escaped within the function. Guide treats any string prefixed with an & character as a variable.