Skip to content

Commit 089ccdf

Browse files
committed
Updated docs and example for reusing templates
1 parent 407430f commit 089ccdf

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

docs/examples.rst

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ This example is printing a basic HTML page with with a dynamic paragraph.
99
:lines: 5-
1010
:linenos:
1111

12-
Reusing templates
13-
-----------------
12+
Caching/Reusing templates
13+
-------------------------
1414

15-
The are two main ways of rendering templates:
15+
The are two ways of rendering templates:
1616

17+
- manually creating a ``Template`` or ``FileTemplate`` object and calling its method
1718
- using one of ``render_...`` methods
18-
- manually creating a ``Template`` object and calling its method
1919

20-
While the first method is simpler, it also compiles the template on every call.
21-
The second method is more efficient when rendering the same template multiple times, as it allows
22-
to reuse the compiled template, at the cost of more memory usage.
2320

24-
Both methods can be used interchangeably, as they both return the same result.
25-
It is up to the user to decide which method is more suitable for a given use case.
21+
By dafault, the ``render_...`` methods cache the template and reuse it on next calls.
22+
This speeds up the rendering process, but also uses more memory.
2623

27-
**Generally, the first method will be sufficient for most use cases.**
24+
If for some reason the caching is not desired, you can disable it by passing ``cache=False`` to
25+
the ``render_...`` method. This will cause the template to be recreated on every call, which is slower,
26+
but uses less memory. This might be useful when rendering a large number of different templates that
27+
might not fit in the memory at the same time or are not used often enough to justify caching them.
2828

29-
It is also worth noting that compiling all used templates using the second method might not be possible,
30-
depending on the project and board used, due to the limited amount of RAM.
3129

3230
.. literalinclude:: ../examples/templateengine_reusing.py
3331
:caption: examples/templateengine_reusing.py
3432
:lines: 5-
35-
:emphasize-lines: 1,16,20
33+
:emphasize-lines: 22,27,34
3634
:linenos:
3735

3836
Expressions
@@ -234,29 +232,20 @@ Autoescaping unsafe characters
234232
------------------------------
235233

236234
Token ``{% autoescape off %} ... {% endautoescape %}`` is used for marking a block of code that should
237-
be not be autoescaped. Consequently using ``{% autoescape off %} ...`` does the opposite and turns
235+
be not be autoescaped. Consequently using ``{% autoescape on %} ...`` does the opposite and turns
238236
the autoescaping back on.
239237

240238
By default the template engine will escape all HTML-unsafe characters in expressions
241239
(e.g. ``<`` will be replaced with ``&lt;``).
242240

243241
Content outside expressions is not escaped and is rendered as-is.
244242

245-
For escaping XML and Markdown, you can use the ``language=`` parameter, both in ``render_...`` methods
246-
and in all ``Template`` constructors.
247-
248243
.. literalinclude:: ../examples/autoescape.html
249244
:caption: examples/autoescape.html
250245
:lines: 7-
251246
:language: html
252247
:linenos:
253248

254-
.. literalinclude:: ../examples/autoescape.md
255-
:caption: examples/autoescape.md
256-
:lines: 5-
257-
:language: markdown
258-
:linenos:
259-
260249
.. literalinclude:: ../examples/templateengine_autoescape.py
261250
:caption: examples/templateengine_autoescape.py
262251
:lines: 5-

examples/templateengine_reusing.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
from adafruit_templateengine import Template
5+
from adafruit_templateengine import Template, render_string
66

77

88
template_string = r"""
@@ -17,8 +17,28 @@
1717
</html>
1818
"""
1919

20-
template = Template(template_string)
20+
other_template_string = r"""
21+
<footer>
22+
<p>Goodbye, {{ context.get("name") or "Anonymous User" }}!</p>
23+
</footer>
24+
"""
25+
26+
# Manually create a Template object
27+
template = Template(template_string) # Creates a template object
28+
print(template.render({"name": "John"})) # Reuses the Template object
29+
print(template.render({"name": "Alex"})) # Reuses the Template object
30+
31+
# Using the `render_string` function
32+
print(
33+
render_string(template_string, {"name": "John"})
34+
) # Creates a new Template object and saves it
35+
print(render_string(template_string, {"name": "Alex"})) # Reuses the Template object
2136

22-
context = {"name": ""} # Put your name here
2337

24-
print(template.render(context))
38+
# Using the `render_string` function, but without caching
39+
print(
40+
render_string(other_template_string, {"name": "John"}, cache=False)
41+
) # Creates a new Template object and does not save it
42+
print(
43+
render_string(other_template_string, {"name": "Alex"}, cache=False)
44+
) # Creates a new Template object a second time and does not save it

0 commit comments

Comments
 (0)