Skip to content

Instantly share code, notes, and snippets.

Last active February 2, 2019 19:58
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save isaac-jordan/2f06f0bf54fc33cdcaab to your computer and use it in GitHub Desktop.
An updated version of django-premailer that works with Django 1.9 and Python 3.
# Put in 'templatetags' folder in your Django app.
from __future__ import absolute_import
from django import template
from django.utils.encoding import smart_text
from django.contrib.staticfiles import finders
from import staticfiles_storage
from django.conf import settings
from premailer import Premailer
register = template.Library()
class PremailerNode(template.Node):
def __init__(self, nodelist, filter_expressions):
self.nodelist = nodelist
self.filter_expressions = filter_expressions
def render(self, context):
rendered_contents = self.nodelist.render(context)
kwargs = PREMAILER_OPTIONS.copy()
for expression in self.filter_expressions:
kwargs.update(base_url=expression.resolve(context, True))
transformed = Premailer(rendered_contents, **kwargs).transform()
return transformed
def premailer(parser, token):
nodelist = parser.parse(('endpremailer',))
# prevent second parsing of endpremailer
args = token.split_contents()[1:]
return PremailerNode(
[parser.compile_filter(arg) for arg in args])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment