Skip to content

Commit d2278d7

Browse files
authored
Merge pull request #4 from michalpokusa/template-syntax-error-rework
Reworked errors in template syntax and overall more validation, caching by default
2 parents 1258e2c + 90b3d33 commit d2278d7

8 files changed

+424
-281
lines changed

adafruit_templateengine.py

Lines changed: 385 additions & 227 deletions
Large diffs are not rendered by default.

docs/api.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
77
.. automodule:: adafruit_templateengine
88
:members:
9-
:inherited-members:

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/autoescape.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
This is a {{ "<b>bold text</b>" }}, because autoescaping is turned off in this block.
2323

2424
{% autoescape on %}
25-
And againg, this is not a {{ "<b>bold text</b>" }},
25+
And again, this is not a {{ "<b>bold text</b>" }},
2626
because in this block autoescaping is turned on again.
2727
{% endautoescape %}
2828
{% endautoescape %}

examples/autoescape.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/templateengine_autoescape.py

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

5-
from adafruit_templateengine import render_template, Language
5+
from adafruit_templateengine import render_template
66

7-
# By default autoescape is enabled for HTML
8-
print("HTML autoescape:")
7+
# By default autoescape is enabled for HTML entities
98
print(render_template("./examples/autoescape.html"))
10-
11-
# But XML and Markdown are also supported
12-
print("Markdown autoescape:")
13-
print(render_template("./examples/autoescape.md", language=Language.MARKDOWN))

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

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ keywords = [
2929
"substitution",
3030
"web",
3131
"html",
32-
"xml",
3332
"escaping",
3433
"syntax",
3534
]

0 commit comments

Comments
 (0)