Skip to main content

Hello!

I’m trying to generate a string consisting of event properties and some other characters. This should then be Base64 encoded and append to a URL. The use case is to create a unique URL in a transactional email, allowing the recipient to login into a returns platform with a single click, without the need to enter order number or email.

The event properties are

{{ event.OrderId|default:'' }}

(e.g. 321645)

and

{{ event.ShippingAddress.Email|default:'' }}

(e.g. example@email.com)

 

I also need to add a few static characters to this - a : between the event values and :company at the end.

With the examples above, the full string would be: 321645:example@email.com:company

I manage to apply the

 | base64_encode

django filter when using non-event properties, but for some reason the preview doesn’t like the event.OrderId property. The ShippingAddress.Email property works fine.

 

This code would seem correct but is not accepted:

{{ event.OrderId|default:'' | append:':' | append:event.ShippingAddress.Email|default:'' | append:':company' | base64_encode }}

This code, on the other hand, is accepted but doesn’t actually pull a value from the OrderId property - so the string only turns out as :example@email.com:company

{{ OrderId|default:'' | append:':' | append:event.ShippingAddress.Email|default:'' | append:':company' | base64_encode }}

The {{ event.OrderId|default:'' }} tag on its own displays the order number fine, but not together with the base64_encode filter. So I’m wondering what I’m missing.

Grateful for any help!

@PeterSandblad  Could you please try using the “with” construct as demonstrated below and confirm whether it functions correctly?

{% with item=event.OrderId|append:':'| append:event.ShippingAddress.Email|append:':company' %}
{{ item| base64_encode}}
{% endwith %}

 


@whereisjad Thanks! That looked like a nifty solution - unfortunately the effect is still the same.

I get an “invalid tag or variable” error when going into “Preview & test”. Tried various different version but I can’t get the string to generate for a start, so base64 encoding it is secondary.


Sorry I am going to share django code I actually tested:

{% with event.OrderId as d %}

{{d|append:":"|append:event.ShippingAddress.Email|append:":company"|base64_encode}}

{% endwith %}

 


@whereisjad I can only get it to work as far as this, which renders the event.OrderId value. If I try to append any value to this, it breaks:

{% with event.OrderId as d %}

{{ d }}

{% endwith %}

This breaks:

{% with event.OrderId as d %}

{{ d|append:":" }}

{% endwith %}

And if I add |base64_encode, nothing is rendered.

{% with event.OrderId as d %}

{{ d|base64_encode }}

{% endwith %}

I’ve tried all version of changing the spacing and replacing “ with ‘

Any other ideas?


I can add that direct inline input works fine to render the full string. But then I want to b64_encode the string 🤔

{{ event.OrderId }}:{{ event.ShippingAddress.Email }}:company

 


OK, with a little help from Perplexity AI I found the solution.

It seems the all-numerical value of the event.OrderId was causing issues. Adding

| stringformat:"s" 

allowed the full string to be generated using this code:

{{ event.OrderId | stringformat:"s" | append:":" | append:event.ShippingAddress.Email | append:":company" | base64_encode }}

Thanks for the help!


Reply