From bb0965c1d2dd0af198c896e146165853e5ecaa8c Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Fri, 12 Jul 2024 08:01:36 +0000 Subject: [PATCH] Fix the MJML template, and document our MJML template tag --- cl8/templates/invite_new_profile.mjml.html | 2 +- ...passwordless_default_token_email.mjml.html | 2 +- cl8/users/templatetags/mjml.py | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cl8/templates/invite_new_profile.mjml.html b/cl8/templates/invite_new_profile.mjml.html index 9e0b540..d180dc9 100644 --- a/cl8/templates/invite_new_profile.mjml.html +++ b/cl8/templates/invite_new_profile.mjml.html @@ -7,7 +7,7 @@ - +

Dear {{ profile.name }},

Welcome to {{ constellation }}.

diff --git a/cl8/templates/passwordless_default_token_email.mjml.html b/cl8/templates/passwordless_default_token_email.mjml.html index 0e69b42..5252c7d 100644 --- a/cl8/templates/passwordless_default_token_email.mjml.html +++ b/cl8/templates/passwordless_default_token_email.mjml.html @@ -7,7 +7,7 @@ - + {% if user %}

Dear {{ user.profile.name }},

{% endif %}

{{ callback_token }} is your code to diff --git a/cl8/users/templatetags/mjml.py b/cl8/users/templatetags/mjml.py index c98748a..850e3b9 100644 --- a/cl8/users/templatetags/mjml.py +++ b/cl8/users/templatetags/mjml.py @@ -1,6 +1,13 @@ +import logging + from django import template from mjml import mjml2html +logger = logging.getLogger(__name__) +# Used for convenience to see the contents of a template +# before is passed as to convert from mjml to html for email +# logger.setLevel(logging.DEBUG) + register = template.Library() @@ -10,7 +17,6 @@ def __init__(self, nodelist): def render(self, context) -> str: mjml_source = self.nodelist.render(context) - breakpoint() return mjml_render(mjml_source) @@ -35,4 +41,15 @@ def mjml(parser, token) -> MJMLRenderNode: def mjml_render(mjml_source: str) -> str: + """ + Render the provided MJML template string, with template context + already added. Returns the html formatted for email clients. + """ + logger.debug(mjml_source) + + # Tip: mjml2html does not give very helpful error messages. + # Paste the `mjml_source` string into a validator like the one + # below if is raising errors + # https://mjml.io/try-it-live/ + return mjml2html(mjml_source)