Use MJML in Django templates
Project description
django-mjml
The simplest way to use MJML in Django templates.
Installation
Requirements:
Django v1.8+
mjml v2.3+ (under node v8)
1. Install mjml.
See https://github.com/mjmlio/mjml#installation and https://mjml.io/documentation/#installation
2. Install django-mjml.
Via pip:
$ pip install django-mjml
Via setuptools:
$ easy_install django-mjml
For install development version use git+https://github.com/liminspace/django-mjml.git@develop instead django-mjml.
3. Set up settings.py in your django project.
INSTALLED_APPS = ( ..., 'mjml', )
Usage
Load mjml in your django template and use mjml tag that will compile mjml to html:
{% load mjml %} {% mjml %} <mjml> <mj-body> <mj-container> <mj-section> <mj-column> <mj-text>Hello world!</mj-text> </mj-column> </mj-section> </mj-container> </mj-body> </mjml> {% endmjml %}
Advanced settings
There are two backend modes for compiling: cmd and tcpserver.
cmd mode
This mode is very simple, slow and used by default.
MJML_BACKEND_MODE = 'cmd' MJML_EXEC_CMD = 'mjml'
You can change MJML_EXEC_CMD and set path to executable mjml file, for example:
MJML_EXEC_CMD = '/home/user/node_modules/.bin/mjml'
Also you can pass addition cmd arguments, for example:
MJML_EXEC_CMD = ['node_modules/.bin/mjml', '--config.minify', 'true', '--config.validationLevel', 'strict']
tcpserver mode
This mode is faster than cmd but it needs run a server process in background.
MJML_BACKEND_MODE = 'tcpserver' MJML_TCPSERVERS = [ ('127.0.0.1', 28101), # host and port ]
You can set several servers and it will be used random one:
MJML_TCPSERVERS = [ ('127.0.0.1', 28101), ('127.0.0.1', 28102), ('127.0.0.1', 28103), ]
You can run servers by commands:
# NODE_PATH=/home/user/node_modules node /home/user/.virtualenv/default/lib/python2.7/site-packages/mjml/node/tcpserver.js --port=28101 --host=127.0.0.1 --touchstop=/tmp/mjmltcpserver.stop
28101 - port, 127.0.0.1 - host, /tmp/mjmltcpserver.stop - file that will stop server after touch.
For daemonize server process you can use, for example, supervisor:
/etc/supervisor/conf.d/mjml.conf [program:mjmltcpserver] user=user environment=NODE_PATH=/home/user/node_modules command=node /home/user/.virtualenv/default/lib/python2.7/site-packages/mjml/node/tcpserver.js --port=28101 --host=127.0.0.1 --touchstop=/tmp/mjmltcpserver.stop --mjml.minify=true --mjml.validationLevel=strict stdout_logfile=/home/user/project/var/log/supervisor/mjml.log autostart=true autorestart=true redirect_stderr=true stopwaitsecs=10 stopsignal=INT
Or you can use docker-compose:
services: mjml-1: image: liminspace/liminspace/mjml-tcpserver:latest restart: always ports: - "28101:28101" mjml-2: image: liminspace/liminspace/mjml-tcpserver:latest restart: always environment: HOST: "0.0.0.0" PORT: "28102" MJML_ARGS: "--mjml.minify=true --mjml.validationLevel=strict" expose: - "28102" ports: - "28102:28102"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_mjml-0.5.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e22c217fdc2776ce1597eb22b615b296ca39a4ee6df28548a6e5e22f6e28e1c |
|
MD5 | 7f6aec31c6f914a4c3fb36a6ced27e32 |
|
BLAKE2b-256 | 855eb5eceed8db91308659d42ed1d4ae5c00d0ec95e02d0da80cd3a0805cd525 |