Recent searches


No recent searches

Using liquid to change html contents of email template

Answered


Posted Apr 05, 2021

Hi all!

I've created a navbar to sit in all the outbound emails we send, as we now have a second support team I would like for them to have their own navbar.

The HTML code is solid and has been tested to ensure it resizes correctly so I have no issues there, where I have been struggling is the use of liquid to change the navbars content based on either a) the support email the request was sent to or the tag assigned to the support request.

From a few articles here this is what should work:

{% if ticket.tags == uk %}
HTML CODE FOR UK NAVBAR

{% else ticket.tags == de %}
HTML CODE FOR DE NAVBAR

{% endif %}

This only sends out the DE navbar to all emails received at any address.  I've tried the following

{% if {ticket.tags} == 'uk' %}

and that either produces the same result or sends nothing out at all (the same result to all emails received).

I also tried to see if I could use the support email address but couldn't find any reference for a liquid tag for that (possibly email.received_at ?).

Is there anyone here who has an idea of what is going on here?  I know the same result can be achieved in brands (which we don't use) and by placing each specific code into relevant triggers/automations but this would mean a lot of work should we need to change some details later on.


0

6

6 comments

First Thoughts

I do not believe that {{email.received_at}} will work. So, your best bet would probably to use tags as you have realized. 

Your main issue

The main issue you are having is that the {{ticket.tags}} returns a string and you are trying to compare "de" with something like this "assignee_has_replied has_attachment not_assigned not_assigned_notification_sent pending_allow_close requester_notified test_send_email"
That is an example list of tags returned when I ran {{ticket.tags}} on my test ticket.

What I would do (off the top of my head)

First things first, we have to split the {{ticket.tags}} into an array that you can loop through and find if any of the individual tags match what you are looking for. 

The first way that came to my mind was to do the following:

{% assign tags = ticket.tags | split:" " %}
{% for tag in tags %}
{{tag}}{% if tag == 'test_send_email' %}=true{% else %}=false{% endif %}
{% endfor %}

This will be a good example of something you can use to test your liquid code as it outputs what is coming in and the results of the if statement. 

What I was returned with was the following:

assignee_has_replied=false
has_attachment=false
not_assigned=false
not_assigned_notification_sent=false
requester_notified=false
test_send_email=true

Explanation

The first line of the code takes the {{ticket.tags}} and splits it according to any spaces. Then it assigns that to a variable called {{tags}}.

The second line loops through {{tags}}

The third line prints the tag and checks to see if it matches the string we are looking for. In my example, that was "test_send_email" if it does not match it returns "=false" as you can see in the output.

The fourth line simply ends the loop.

Final Thoughts

To use this answer, you will need to remove the printing out of the tag and what happens when it matches and when it does not.

Another thing I would look at in your case would be to use the liquid tag of {% case %}. The information regarding this is here: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers#case-statement
You will still need to split the {{ticket.tags}} into an array, I believe.

Let me know if you have any questions.

0


Hi @..., thanks for taking the time to help!

I tried this:

{% assign tags = ticket.tags | split:" " %}
{% for tag in tags %}
{{tag}}{% if tag == 'test_send_email' %}=true{% else %}=false{% endif %}
{% endfor %}

 in the email template and got a blank email, should this have returned

assignee_has_replied=false
has_attachment=false
not_assigned=false
not_assigned_notification_sent=false
requester_notified=false
test_send_email=true

in the email body?

Am I right in thinking the code for my situation would look something like

{% assign tags = ticket.tags | split:" " %}
{% for tag in tags %}
{{tag}}{% if tag == 'uk' %}= HTML CODE FOR UK{% else %}= HTML CODE FOR DE{% endif %}
{% endfor %}

I was sent the case-statement link by Zendesk Live Chat but couldn't get that to work either, I'm either missing some very obvious or perhaps the email template doesn't support this?

0


No problem.

All of this should work pretty much anywhere the placeholders actually provide data. I.e. email templates. Also, that is explicitly where I tested it before I sent that message.

Let’s start at the beginning and make sure we cover our bases. I am not at my computer at the moment but we should still be able to get through this.

First thing: where exactly are you trying to input this code? I was under the assumption you were adding it to the trigger that sends out the email. If that is not correct, let me know.

Second thing: let’s confirm that the ticket you are testing actually has tags. If it doesn’t have any tags, that may be what is causing your issue as there is nothing to output.

Third thing: let’s back up and confirm that the placeholder {{ticket.tags}} is working correctly for you. For now, remove the code from this post and simply replace it with {{ticket.tags}} if that returns blank then that will be the first thing we need to troubleshoot. That is what the first and second things are trying to establish.

0


First thing: where exactly are you trying to input this code? I was under the assumption you were adding it to the trigger that sends out the email. If that is not correct, let me know.

The code is being used in the email template (settings > email > email template), sorry if I didn't make this clear!

Second thing: let’s confirm that the ticket you are testing actually has tags. If it doesn’t have any tags, that may be what is causing your issue as there is nothing to output.

I've set up a dedicate email to act as the uk email with a trigger to assign the uk tag.  I've also set up another dedicated email to act the de/germany email with a trigger to assign the de tag.

When I open the ticket I can see that the tag has been applied successfully.

Third thing: let’s back up and confirm that the placeholder {{ticket.tags}} is working correctly for you. For now, remove the code from this post and simply replace it with {{ticket.tags}} if that returns blank then that will be the first thing we need to troubleshoot. That is what the first and second things are trying to establish.

When {{ticket.tags}} is added to the email template it returns nothing, but when inside the triggers it returns either uk or de as expected.

Edit: Forgot to mention when

{% assign tags = ticket.tags | split:" " %}
{% for tag in tags %}
{{tag}}{% if tag == 'uk' %}=true{% else %}=false{% endif %}
{% endfor %}

Was added to the trigger for uk emails, it displays uk=true so it looks like it can pick up the tags from inside the trigger

0


The code is being used in the email template (settings > email > email template), sorry if I didn't make this clear!

Ok. This is starting to make a lot more sense. 

It looks like when adding the placeholder to the actual email template it is for some reason not replacing the {{ticket.tags}}. Personally, I have not tried that but I can at some point today to verify if it is also the case on my end. 

When {{ticket.tags}} is added to the email template it returns nothing, but when inside the triggers it returns either uk or de as expected.

Thankfully, when adding it to the trigger it looks like it is replacing it correctly and the liquid code is working as anticipated as you returned "uk=true"

Moving Forward

If the goal is to get it working in the email template and not the trigger then my next thoughts would be to see how we can pass the necessary information to the email template. It appears that either the placeholder is not being replaced correctly or that the information is not available at that context level, I would try and see if we can manually pass the information from the trigger level. 

What I would do

What I would do is start by putting some conditions in your email template. Something like if UK then show the UK version if DE show the german version if null then show default version. This should allow for showing the correct version if provided but defaulting to one if not provided.

{% if tag == 'uk' %}= HTML CODE FOR UK{% else %}= HTML CODE FOR DE{% endif %}

That is where you can start testing passing a variable to the email template level. To be honest, I would keep the code pretty much the same and simply split it between the 2. 

This would go in the trigger:

{% assign tags = ticket.tags | split:" " %}

Something similar to this would go in the email template:

{{tags}}
{% for tag in tags %}
{{tag}}{% if tag == 'uk' %}=true{% else %}=false{% endif %}
{% endfor %}

This will show us if we can pass our necessary variable to the email template. 

Explanation

We now know that the {{ticket.tags}} and the splitting works at the trigger level. We also can be reasonably sure that conditionals (and liquid code) should work at the email template level. Therefore, we need to simply pass the necessary variables to the email template level. i.e. setting a new variable at the trigger level and accessing it at the email template level.

0


@...

I found the most convoluted way to accomplish what you are looking for. 

Convoluted Solution:

At the very bottom of each trigger that needs to activate something in the email template (read: custom header) insert an HTML element that does not show on the browser with an attribute that includes the data you require. This will allow you to use liquid code to parse the invisible element at the email template level.

Example:

In the trigger at the very bottom add something similar to the following: (all of the spacing is important)

<div title=" {{ ticket.tags | split:" " | join:" " }} " style="display:none;"></div>

In the email template add something similar to the following:

{% assign contents = content | escape | split:" " %}
{% for k in contents %}
{% if k == 'test_send_email' %}true{% else %}false{% endif %}
{% endfor %}

Replace the "test_send_email" with whatever tag you are looking for.
Replace the "true" for whatever you want to happen if true.
Replace the "false" for whatever you want to happen if false. Or remove it entirely.

Explanation:

Apparently, Zendesk does not allow access to any placeholders in the email template. Outlined here

So, to send the tags to the email template level without showing to the email recipient we need to include the tags in some HTML that will not be seen by the recipient. I tried simply putting it in a comment but those seem to be removed by some internal code only Zendesk has access to. In my example, I added a "div" element with the "display:none;" style included. I then added a title attribute to the hidden "div" that included the tags for the ticket. 

Next, we create an array that is created by HTML escaping {{content}} and splitting by space characters. We can then loop through the array and see if there is a match and do something based on the matches. 

Notes:

You can probably avoid false positives by removing everything outside of the "div" when creating the array.

0


Post is closed for comments.

Didn't find what you're looking for?

New post