Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{{ base_url }} doesn't work in email templates #217

Open
greyskin opened this issue Sep 20, 2022 · 2 comments
Open

{{ base_url }} doesn't work in email templates #217

greyskin opened this issue Sep 20, 2022 · 2 comments

Comments

@greyskin
Copy link

greyskin commented Sep 20, 2022

On a number of sites we manage, we have set up various email templates to be delivered to our clients' customers when they perform certain actions on the site, e.g. register for an account, place an order, etc.

We have been using {{ base_url }} in conjunction with the {% path_to 'page-handle' %} tag in our email templates and we have realised that this doesn't work as we expected. Specifically, it only renders the relative page link.

Example:

I've done some testing and have removed {{ base_url }} entirely and the result is the same. I expect our email client (Gmail) is adding the URL protocol (http://) automatically, which would indicate that {{ base_url }} isn't being rendered at all.

Obviously, we can hard-code the URL protocol and site domain for every site, but this is far from ideal. Is this expected behaviour?

@greyskin greyskin reopened this Sep 21, 2022
@greyskin greyskin changed the title {% path_to page-handle %} doesn't work in email templates {{ base_url }} doesn't work in email templates Sep 21, 2022
@did
Copy link
Member

did commented Sep 21, 2022

@greyskin thanks for bringing this topic up.

First of all,{{ base_url }} is an alias pointing to the base_url method of the rack HTTP request. Here is the current implementation:

def base_url
  "#{scheme}://#{host_with_port}"
end

We do have an "issue" with base_url in the context of email sending. When we send an email which is going to use a page liquid template, we use a different rendering mechanism, different from the classical HTTP one. And we don't pass the rack HTTP request since email can be delivered asynchronously.
So, the result is that {{ base_url }} will return indeed an empty string.

My recommendation is to write something like this in email page templates:

<a href="{{ site.domains | first }}{% path_to 'contact' %}">Contact us</a>

Regarding assets, I declare my css like this:

<link href="{{ 'email.css' | stylesheet_url | absolute_url }}" media="screen" rel="stylesheet" type="text/css">

@greyskin
Copy link
Author

Thanks @did,

That's very useful information. I've implemented your suggestion and it works as expected.


For others who might stumble across this, one thing to note is that {{ site.domains }} will render nothing if the email is triggered from a staging domain (e.g. station.locomotive.works). To ensure complete working functionality for our customers during the sign-off phase of a project, I would implement something like:

{% if site.domains == empty %}
    {% assign baseURL = 'https://station.locomotive.works' %}
{% else %}
    {% assign baseURL = site.domains | first | prepend: 'https://' %}
{% endif %}

Then:

<a href="{{ baseURL }}{% path_to 'contact' %}">Contact us</a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants