A couple of weeks ago I released version 0.8 of django-render-block, this was followed up with a 0.8.1 to fix a regression.
django-render-block is a small library that allows you render a specific block from a Django (or Jinja) template, this is frequently used for emails when you want multiple pieces of an email together in a single template (e.g. the subject, HTML body, and text body), but they need to be rendered separately before sending.
See the example below:
{% block subject %}Hello from django-render-block!{% endblock %}
{% block text %}
Hello!
This is the text email body!
{% endblock %}
{% block html %}
<h1>Hello!</h1>
<p>This is the <b>HTML</b> email body!</p>
{% endblock %}
Using django-render-block, each block can be rendered separately and then passed into Django’s email system:
from django.core.mail import send_mail
from render_block import render_block_to_string
subject = render_block_to_string('email.html', 'subject')
text_body = render_block_to_string('email.html', 'text')
html_body = render_block_to_string('email.html', 'html')
send_mail(
subject,
text_body,
html_message=html_body,
)
This particular release of django-render-block includes support for passing a fully instantiated Context (instead of a simple dict) into render_block_to_string. This allows the caller to control whether autoescape is enabled and a few other features.
The full changelog is included below: