Skip to content

Commit 0815986

Browse files
committed
Docs: Add templates syntax explanation and usage
1 parent 8b3df9f commit 0815986

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

docs/source/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Welcome to django-templated-mail documentation!
1111

1212
getting_started
1313
settings
14+
templates_syntax
15+
sample_usage
1416

1517
Indices and tables
1618
==================

docs/source/sample_usage.rst

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Sample usage
2+
============
3+
4+
At first let's discuss the simplest possible use case, where you just wish to
5+
send an email to a given address and using the given template.
6+
7+
.. code-block:: python
8+
9+
from templated_mail.mail import BaseEmailMessage
10+
11+
BaseEmailMessage(template_name='email.html').send(to=['foo@bar.tld'])
12+
13+
This one-liner will do all of the work required to render proper template blocks
14+
and assign the results to proper email pieces. It will also determine appropriate
15+
content type (including support for MIME) and send the output message to provided
16+
list of email address'.
17+
18+
You might also wish to define your own subclass of
19+
``templated_mail.mail.BaseEmailMessage`` to customize a thing or two.
20+
What might be most interesting for you is the ``get_context_data`` method,
21+
which returns context used during template rendering.
22+
23+
.. code-block:: python
24+
25+
class MyEmailMessage(BaseEmailMessage):
26+
def get_context_data(self):
27+
context = super(MyEmailMessage, self).get_context_data()
28+
context['foo'] = 'bar'
29+
return context
30+
31+
You might also provide custom context data using the ``context`` parameter.
32+
33+
.. code-block:: python
34+
35+
from templated_mail.mail import BaseEmailMessage
36+
37+
BaseEmailMessage(context={'foo': 'bar'}, template_name='email.html').send(to=['foo@bar.tld'])
38+
39+
In other cases you might notice that some of your emails use common ``template_name``
40+
and so to save some space you might wish to override the base class' attribute.
41+
42+
.. code-block:: python
43+
44+
class MyEmailMessage(BaseEmailMessage):
45+
template_name = 'email.html'

docs/source/templates_syntax.rst

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Templates syntax
2+
================
3+
4+
Email templates can be built using three simple blocks:
5+
6+
- ``subject`` - used for subject of an email message
7+
- ``text_body`` - used for plaintext body of an email message (not required)
8+
- ``html_body`` - used for html body of an email message (not required)
9+
10+
Examples
11+
--------
12+
13+
.. code-block:: html
14+
15+
{% block subject %}Text and HTML mail subject{% endblock %}
16+
17+
{% block text_body %}Foobar email content{% endblock %}
18+
19+
.. code-block:: html
20+
21+
{% block subject %}Text and HTML mail subject{% endblock %}
22+
23+
{% block text_body %}Foobar email content{% endblock %}
24+
{% block html_body %}<p>Foobar email content</p>{% endblock %}

0 commit comments

Comments
 (0)