Giter Site home page Giter Site logo

Integrate mjml-python? about django-mjml HOT 3 OPEN

liminspace avatar liminspace commented on September 28, 2024 7
Integrate mjml-python?

from django-mjml.

Comments (3)

blopker avatar blopker commented on September 28, 2024 3

For anyone who wants to try this now, I've copied the template tag from this library and copied it into my own project. There's not much to it when you don't have to talk to a separate process:

In templatetags/mjml.py:

from django import template
from mjml import mjml2html

register = template.Library()


class MJMLRenderNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context) -> str:
        mjml_source = self.nodelist.render(context)
        return mjml_render(mjml_source)


@register.tag
def mjml(parser, token) -> MJMLRenderNode:
    """
    Compile MJML template after render django template.

    Usage:
        {% mjml %}
            .. MJML template code ..
        {% endmjml %}
    """
    nodelist = parser.parse(("endmjml",))
    parser.delete_first_token()
    tokens = token.split_contents()
    if len(tokens) != 1:
        raise template.TemplateSyntaxError("'%r' tag doesn't receive any arguments." % tokens[0])
    return MJMLRenderNode(nodelist)


def mjml_render(mjml_source: str) -> str:
    return mjml2html(mjml_source)

from django-mjml.

liminspace avatar liminspace commented on September 28, 2024 1

@blopker I think you can use mjml-python to create a HTTP or a TCP server and use a proper config to use that. It's better to use external service for rendering emails especially if you send a lot of emails.
But your idea is a good way to use mjml easier for smaller projects. I'm thinking about supporting customizable classes to make supporting alternative mjml-backends more flexible and allow developers use custom backends without making changes in django-mjml.

from django-mjml.

aplex avatar aplex commented on September 28, 2024 1

Tried @blopker version. Works well so far, and about 100x faster than cmd implementation of django-mjml.
0.006 seconds per render vs 0.9 seconds

from django-mjml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.