question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jinja variables in hrefs are HTML encoded. Outside of hrefs they're not encoded.

See original GitHub issue

Premailer version 2.5.0.

I have an HTML template with Jinja variables in it. I’d like for these Jinja variables to remain after using premailer, so I can cache the premailed version and do variable substitution in a loop.

For variables in hrefs, like <a href="{{ SURVEY_LINK }}" class="btn-primary">Start Survey</a>

Premailer HTML encodes the braces to their HTML-encoded entities: <a href="%7B%7B%20SURVEY_LINK%20%7D%7D" class="btn-primary"...

Likewise, brackets, [ and ] in href quotes are escaped, which is problematic for my use case as well.

I’d like to use premailer on my Jinja email templates but because of this HTML encoding issue I’m not able to.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:4
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
darklowcommented, Jul 14, 2014

@skyler Looks like it is lxml not premailer related issue. See here: http://stackoverflow.com/questions/4684614/is-there-a-way-to-disable-urlencoding-of-anchor-attributes-in-lxml

Until better solution is available, i used simple replace to fix the problem with href="{{ }}". After string has been transformed fix it back:

mapping = (('%7B%7B%20', '{{ '), ('%20%7D%7D', ' }}'))
for k, v in mapping:
    s = s.replace(k, v)
1reaction
jdq22commented, Jun 22, 2017

I ran into this issue as well with SendGrid marketing campaign emails, specifically for [Weblink] and [unsubscribe].

I ended up chaining two .replace().replace() to .transform() to look for the HTML encoded weblink and unsubscribe strings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced: How to use href in Jinja2 - Stack Overflow
One way to add the id (from the Python variable a.id to your URL is to use the % string formatting operator, like...
Read more >
Template Syntax
Template Syntax. This vignette describes the template syntax supported by jinjar, following the structure of the Jinja Template Designer Documentation.
Read more >
API — Jinja Documentation (2.9.x)
This document describes the API to Jinja2 and not the template language. ... For Jinja2 the default encoding of templates is assumed to...
Read more >
Jinja Filters - Bloomreach Documentation
Using String filters on non-String variables casts the variables to ... {# generate base64url-encoded strings out of JSONs #} {# replace ...
Read more >
Primer on Jinja Templating - Real Python
Render a Jinja template in Flask; Use for loops and conditional statements with Jinja; Nest Jinja templates; Modify variables in Jinja with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found