-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadafruit_templateengine.py
906 lines (706 loc) · 29.5 KB
/
adafruit_templateengine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
# SPDX-FileCopyrightText: Copyright (c) 2023 Michał Pokusa, Tim Cocks
#
# SPDX-License-Identifier: MIT
"""
`adafruit_templateengine`
================================================================================
Templating engine to substitute variables into a template string.
Templates can also include conditional logic and loops. Often used for web pages.
* Author(s): Michał Pokusa, Tim Cocks
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://circuitpython.org/downloads
"""
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TemplateEngine.git"
try:
from typing import Any, Generator
except ImportError:
pass
import os
import re
try:
from sys import implementation
if implementation.name == "circuitpython" and implementation.version < (9, 0, 0):
print(
"Warning: adafruit_templateengine requires CircuitPython 9.0.0, as previous versions"
" will have limited functionality when using block comments and non-ASCII characters."
)
finally:
# Unimport sys to prevent accidental use
del implementation
class Language: # pylint: disable=too-few-public-methods
"""
Enum-like class that contains languages supported for escaping.
"""
HTML = "html"
"""HTML language"""
XML = "xml"
"""XML language"""
MARKDOWN = "markdown"
"""Markdown language"""
class Token: # pylint: disable=too-few-public-methods
"""Stores a token with its position in a template."""
def __init__(self, template: str, start_position: int, end_position: int):
self.template = template
self.start_position = start_position
self.end_position = end_position
self.content = template[start_position:end_position]
def safe_html(value: Any) -> str:
"""
Encodes unsafe symbols in ``value`` to HTML entities and returns the string that can be safely
used in HTML.
Examples::
safe_html('<a href="https://circuitpython.org/">CircuitPython</a>')
# <a href="https://circuitpython.org/">...
safe_html(10 ** (-10))
# 1e−10
"""
def _replace_amp_or_semi(match: re.Match):
return "&" if match.group(0) == "&" else ";"
return (
# Replace initial & and ; together
re.sub(r"&|;", _replace_amp_or_semi, str(value))
# Replace other characters
.replace('"', """)
.replace("_", "_")
.replace("-", "−")
.replace(",", ",")
.replace(":", ":")
.replace("!", "!")
.replace("?", "?")
.replace(".", ".")
.replace("'", "'")
.replace("(", "(")
.replace(")", ")")
.replace("[", "[")
.replace("]", "]")
.replace("{", "{")
.replace("}", "}")
.replace("@", "@")
.replace("*", "*")
.replace("/", "/")
.replace("\\", "\")
.replace("#", "#")
.replace("%", "%")
.replace("`", "`")
.replace("^", "^")
.replace("+", "+")
.replace("<", "<")
.replace("=", "=")
.replace(">", ">")
.replace("|", "|")
.replace("~", "˜")
.replace("$", "$")
)
def safe_xml(value: Any) -> str:
"""
Encodes unsafe symbols in ``value`` to XML entities and returns the string that can be safely
used in XML.
Example::
safe_xml('<a href="https://circuitpython.org/">CircuitPython</a>')
# <a href="https://circuitpython.org/">CircuitPython</a>
"""
return (
str(value)
.replace("&", "&")
.replace('"', """)
.replace("'", "'")
.replace("<", "<")
.replace(">", ">")
)
def safe_markdown(value: Any) -> str:
"""
Encodes unsafe symbols in ``value`` and returns the string that can be safely used in Markdown.
Example::
safe_markdown('[CircuitPython](https://circuitpython.org/)')
# \\[CircuitPython\\]\\(https://circuitpython.org/\\)
"""
return (
str(value)
.replace("_", "\\_")
.replace("-", "\\-")
.replace("!", "\\!")
.replace("(", "\\(")
.replace(")", "\\)")
.replace("[", "\\[")
.replace("]", "\\]")
.replace("*", "\\*")
.replace("*", "\\*")
.replace("&", "\\&")
.replace("#", "\\#")
.replace("`", "\\`")
.replace("+", "\\+")
.replace("<", "\\<")
.replace(">", "\\>")
.replace("|", "\\|")
.replace("~", "\\~")
)
_EXTENDS_PATTERN = re.compile(r"{% extends '.+?' %}|{% extends \".+?\" %}")
_BLOCK_PATTERN = re.compile(r"{% block \w+? %}")
_INCLUDE_PATTERN = re.compile(r"{% include '.+?' %}|{% include \".+?\" %}")
_HASH_COMMENT_PATTERN = re.compile(r"{# .+? #}")
_BLOCK_COMMENT_PATTERN = re.compile(
r"{% comment ('.*?' |\".*?\" )?%}[\s\S]*?{% endcomment %}"
)
_TOKEN_PATTERN = re.compile(r"{{ .+? }}|{% .+? %}")
_LSTRIP_BLOCK_PATTERN = re.compile(r"\n( )+$")
def _find_extends(template: str):
return _EXTENDS_PATTERN.search(template)
def _find_block(template: str):
return _BLOCK_PATTERN.search(template)
def _find_include(template: str):
return _INCLUDE_PATTERN.search(template)
def _find_named_endblock(template: str, name: str):
return re.search(r"{% endblock " + name + r" %}", template)
def _underline_token_in_template(
token: Token, *, lines_around: int = 5, symbol: str = "^"
) -> str:
"""
Return ``number_of_lines`` lines before and after the token, with the token content underlined
with ``symbol`` e.g.:
```html
[8 lines skipped]
Shopping list:
<ul>
{% for item in context["items"] %}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<li>{{ item["name"] }} - ${{ item["price"] }}</li>
{% empty %}
[5 lines skipped]
```
"""
template_before_token = token.template[: token.start_position]
if (skipped_lines := template_before_token.count("\n") - lines_around) > 0:
template_before_token = f"[{skipped_lines} lines skipped]\n" + "\n".join(
template_before_token.split("\n")[-(lines_around + 1) :]
)
template_after_token = token.template[token.end_position :]
if (skipped_lines := template_after_token.count("\n") - lines_around) > 0:
template_after_token = (
"\n".join(template_after_token.split("\n")[: (lines_around + 1)])
+ f"\n[{skipped_lines} lines skipped]"
)
lines_before_line_with_token = template_before_token.rsplit("\n", 1)[0]
line_with_token = (
template_before_token.rsplit("\n", 1)[-1]
+ token.content
+ template_after_token.split("\n")[0]
)
line_with_underline = (
" " * len(template_before_token.rsplit("\n", 1)[-1])
+ symbol * len(token.content)
+ " " * len(template_after_token.split("\n")[0])
)
lines_after_line_with_token = template_after_token.split("\n", 1)[-1]
return "\n".join(
[
lines_before_line_with_token,
line_with_token,
line_with_underline,
lines_after_line_with_token,
]
)
def _exists_and_is_file(path: str) -> bool:
try:
return (os.stat(path)[0] & 0b_11110000_00000000) == 0b_10000000_00000000
except OSError:
return False
def _resolve_includes(template: str):
while (include_match := _find_include(template)) is not None:
template_path = include_match.group(0)[12:-4]
# TODO: Restrict include to specific directory
if not _exists_and_is_file(template_path):
raise OSError(
f"Include template not found: {template_path}\n\n"
+ _underline_token_in_template(
Token(template, include_match.start(), include_match.end())
)
)
# Replace the include with the template content
with open(template_path, "rt", encoding="utf-8") as template_file:
template = (
template[: include_match.start()]
+ template_file.read()
+ template[include_match.end() :]
)
return template
def _resolve_includes_blocks_and_extends(template: str):
block_replacements: "dict[str, str]" = {}
# Processing nested child templates
while (extends_match := _find_extends(template)) is not None:
extended_template_name = extends_match.group(0)[12:-4]
# Load extended template
with open(
extended_template_name, "rt", encoding="utf-8"
) as extended_template_file:
extended_template = extended_template_file.read()
offset = extends_match.end()
# Resolve includes
template = _resolve_includes(template)
# Save block replacements
while (block_match := _find_block(template[offset:])) is not None:
block_name = block_match.group(0)[9:-3]
endblock_match = _find_named_endblock(template[offset:], block_name)
if endblock_match is None:
raise SyntaxError(
"Missing {% endblock %}:\n\n"
+ _underline_token_in_template(
Token(
template,
offset + block_match.start(),
offset + block_match.end(),
)
)
)
block_content = template[
offset + block_match.end() : offset + endblock_match.start()
]
# Check for unsupported nested blocks
if (nested_block_match := _find_block(block_content)) is not None:
raise SyntaxError(
"Nested blocks are not supported:\n\n"
+ _underline_token_in_template(
Token(
template,
offset + block_match.end() + nested_block_match.start(),
offset + block_match.end() + nested_block_match.end(),
)
)
)
if block_name in block_replacements:
block_replacements[block_name] = block_replacements[block_name].replace(
r"{{ block.super }}", block_content
)
else:
block_replacements.setdefault(block_name, block_content)
offset += endblock_match.end()
template = extended_template
# Resolve includes in top-level template
template = _resolve_includes(template)
return _replace_blocks_with_replacements(template, block_replacements)
def _replace_blocks_with_replacements(template: str, replacements: "dict[str, str]"):
# Replace blocks in top-level template
while (block_match := _find_block(template)) is not None:
block_name = block_match.group(0)[9:-3]
# Self-closing block tag without default content
if (endblock_match := _find_named_endblock(template, block_name)) is None:
replacement = replacements.get(block_name, "")
template = (
template[: block_match.start()]
+ replacement
+ template[block_match.end() :]
)
# Block with default content
else:
block_content = template[block_match.end() : endblock_match.start()]
# Check for unsupported nested blocks
if (nested_block_match := _find_block(block_content)) is not None:
raise SyntaxError(
"Nested blocks are not supported:\n\n"
+ _underline_token_in_template(
Token(
template,
block_match.end() + nested_block_match.start(),
block_match.end() + nested_block_match.end(),
)
)
)
# No replacement for this block, use default content
if block_name not in replacements:
template = (
template[: block_match.start()]
+ block_content
+ template[endblock_match.end() :]
)
# Replace default content with replacement
else:
replacement = replacements[block_name].replace(
r"{{ block.super }}", block_content
)
template = (
template[: block_match.start()]
+ replacement
+ template[endblock_match.end() :]
)
return template
def _find_hash_comment(template: str):
return _HASH_COMMENT_PATTERN.search(template)
def _find_block_comment(template: str):
return _BLOCK_COMMENT_PATTERN.search(template)
def _remove_comments(
template: str,
*,
trim_blocks: bool = True,
lstrip_blocks: bool = True,
):
def _remove_matched_comment(template: str, comment_match: re.Match):
text_before_comment = template[: comment_match.start()]
text_after_comment = template[comment_match.end() :]
if text_before_comment:
if lstrip_blocks:
if _token_is_on_own_line(text_before_comment):
text_before_comment = text_before_comment.rstrip(" ")
if text_after_comment:
if trim_blocks:
if text_after_comment.startswith("\n"):
text_after_comment = text_after_comment[1:]
return text_before_comment + text_after_comment
# Remove hash comments: {# ... #}
while (comment_match := _find_hash_comment(template)) is not None:
template = _remove_matched_comment(template, comment_match)
# Remove block comments: {% comment %} ... {% endcomment %}
while (comment_match := _find_block_comment(template)) is not None:
template = _remove_matched_comment(template, comment_match)
return template
def _find_token(template: str):
return _TOKEN_PATTERN.search(template)
def _token_is_on_own_line(text_before_token: str) -> bool:
return _LSTRIP_BLOCK_PATTERN.search(text_before_token) is not None
def _create_template_rendering_function( # pylint: disable=,too-many-locals,too-many-branches,too-many-statements
template: str,
language: str = Language.HTML,
*,
trim_blocks: bool = True,
lstrip_blocks: bool = True,
function_name: str = "__template_rendering_function",
context_name: str = "context",
dry_run: bool = False,
) -> "Generator[str] | str":
# Resolve includes, blocks and extends
template = _resolve_includes_blocks_and_extends(template)
# Remove comments
template = _remove_comments(template)
# Create definition of the template function
function_string = f"def {function_name}({context_name}):\n"
indent, indentation_level = " ", 1
# Keep track of the template state
nested_if_statements: "list[Token]" = []
nested_for_loops: "list[Token]" = []
nested_while_loops: "list[Token]" = []
nested_autoescape_modes: "list[Token]" = []
last_token_was_block = False
offset = 0
# Resolve tokens
while (token_match := _find_token(template[offset:])) is not None:
token = Token(
template,
offset + token_match.start(),
offset + token_match.end(),
)
# Add the text before the token
if text_before_token := template[offset : offset + token_match.start()]:
if lstrip_blocks and token.content.startswith(r"{% "):
if _token_is_on_own_line(text_before_token):
text_before_token = text_before_token.rstrip(" ")
if trim_blocks:
if last_token_was_block and text_before_token.startswith("\n"):
text_before_token = text_before_token[1:]
if text_before_token:
function_string += (
indent * indentation_level + f"yield {repr(text_before_token)}\n"
)
else:
function_string += indent * indentation_level + "pass\n"
# Token is an expression
if token.content.startswith(r"{{ "):
last_token_was_block = False
if nested_autoescape_modes:
autoescape = nested_autoescape_modes[-1].content[14:-3] == "on"
else:
autoescape = True
# Expression should be escaped with language-specific function
if autoescape:
function_string += (
indent * indentation_level
+ f"yield safe_{language.lower()}({token.content[3:-3]})\n"
)
# Expression should not be escaped
else:
function_string += (
indent * indentation_level + f"yield str({token.content[3:-3]})\n"
)
# Token is a statement
elif token.content.startswith(r"{% "):
last_token_was_block = True
# Token is a some sort of if statement
if token.content.startswith(r"{% if "):
function_string += (
indent * indentation_level + f"{token.content[3:-3]}:\n"
)
indentation_level += 1
nested_if_statements.append(token)
elif token.content.startswith(r"{% elif "):
indentation_level -= 1
function_string += (
indent * indentation_level + f"{token.content[3:-3]}:\n"
)
indentation_level += 1
elif token.content == r"{% else %}":
indentation_level -= 1
function_string += indent * indentation_level + "else:\n"
indentation_level += 1
elif token.content == r"{% endif %}":
indentation_level -= 1
if not nested_if_statements:
raise SyntaxError(
"Missing {% if ... %}\n\n" + _underline_token_in_template(token)
)
nested_if_statements.pop()
# Token is a for loop
elif token.content.startswith(r"{% for "):
function_string += (
indent * indentation_level + f"{token.content[3:-3]}:\n"
)
indentation_level += 1
nested_for_loops.append(token)
elif token.content == r"{% empty %}":
indentation_level -= 1
last_forloop_iterable = (
nested_for_loops[-1].content[3:-3].split(" in ", 1)[1]
)
function_string += (
indent * indentation_level + f"if not {last_forloop_iterable}:\n"
)
indentation_level += 1
elif token.content == r"{% endfor %}":
indentation_level -= 1
if not nested_for_loops:
raise SyntaxError(
"Missing {% for ... %}\n\n"
+ _underline_token_in_template(token)
)
nested_for_loops.pop()
# Token is a while loop
elif token.content.startswith(r"{% while "):
function_string += (
indent * indentation_level + f"{token.content[3:-3]}:\n"
)
indentation_level += 1
nested_while_loops.append(token)
elif token.content == r"{% endwhile %}":
indentation_level -= 1
if not nested_while_loops:
raise SyntaxError(
"Missing {% while ... %}\n\n"
+ _underline_token_in_template(token)
)
nested_while_loops.pop()
# Token is a Python code
elif token.content.startswith(r"{% exec "):
expression = token.content[8:-3]
function_string += indent * indentation_level + f"{expression}\n"
# Token is autoescape mode change
elif token.content.startswith(r"{% autoescape "):
mode = token.content[14:-3]
if mode not in ("on", "off"):
raise ValueError(f"Unknown autoescape mode: {mode}")
nested_autoescape_modes.append(token)
elif token.content == r"{% endautoescape %}":
if not nested_autoescape_modes:
raise SyntaxError(
"Missing {% autoescape ... %}\n\n"
+ _underline_token_in_template(token)
)
nested_autoescape_modes.pop()
else:
raise SyntaxError(
f"Unknown token type: {token.content}\n\n"
+ _underline_token_in_template(token)
)
else:
raise SyntaxError(
f"Unknown token type: {token.content}\n\n"
+ _underline_token_in_template(token)
)
# Move offset to the end of the token
offset += token_match.end()
# Checking for unclosed blocks
if len(nested_if_statements) > 0:
last_if_statement = nested_if_statements[-1]
raise SyntaxError(
"Missing {% endif %}\n\n" + _underline_token_in_template(last_if_statement)
)
if len(nested_for_loops) > 0:
last_for_loop = nested_for_loops[-1]
raise SyntaxError(
"Missing {% endfor %}\n\n" + _underline_token_in_template(last_for_loop)
)
if len(nested_while_loops) > 0:
last_while_loop = nested_while_loops[-1]
raise SyntaxError(
"Missing {% endwhile %}\n\n" + _underline_token_in_template(last_while_loop)
)
# No check for unclosed autoescape blocks, as they are optional and do not result in errors
# Add the text after the last token (if any)
text_after_last_token = template[offset:]
if text_after_last_token:
if trim_blocks and text_after_last_token.startswith("\n"):
text_after_last_token = text_after_last_token[1:]
function_string += (
indent * indentation_level + f"yield {repr(text_after_last_token)}\n"
)
# If dry run, return the template function string
if dry_run:
return function_string
# Create and return the template function
exec(function_string) # pylint: disable=exec-used
return locals()[function_name]
def _yield_as_sized_chunks(
generator: "Generator[str]", chunk_size: int
) -> "Generator[str]":
"""Yields resized chunks from the ``generator``."""
# Yield chunks with a given size
chunk = ""
for item in generator:
chunk += item
if chunk_size <= len(chunk):
yield chunk[:chunk_size]
chunk = chunk[chunk_size:]
# Yield the last chunk
if chunk:
yield chunk
class Template:
"""
Class that loads a template from ``str`` and allows to rendering it with different contexts.
"""
_template_function: "Generator[str]"
def __init__(self, template_string: str, *, language: str = Language.HTML) -> None:
"""
Creates a reusable template from the given template string.
For better performance, instantiate the template in global scope and reuse it as many times.
If memory is a concern, instantiate the template in a function or method that uses it.
By default, the template is rendered as HTML. To render it as XML or Markdown, use the
``language`` parameter.
:param str template_string: String containing the template to be rendered
:param str language: Language for autoescaping. Defaults to HTML
"""
self._template_function = _create_template_rendering_function(
template_string, language
)
def render_iter(
self, context: dict = None, *, chunk_size: int = None
) -> "Generator[str]":
"""
Renders the template using the provided context and returns a generator that yields the
rendered output.
:param dict context: Dictionary containing the context for the template
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
the template in chunks sized specifically for the given template
Example::
template = ... # r"Hello {{ name }}!"
list(template.render_iter({"name": "World"}))
# ['Hello ', 'World', '!']
list(template.render_iter({"name": "CircuitPython"}, chunk_size=3))
# ['Hel', 'lo ', 'Cir', 'cui', 'tPy', 'tho', 'n!']
"""
return (
_yield_as_sized_chunks(self._template_function(context or {}), chunk_size)
if chunk_size is not None
else self._template_function(context or {})
)
def render(self, context: dict = None) -> str:
"""
Render the template with the given context.
:param dict context: Dictionary containing the context for the template
Example::
template = ... # r"Hello {{ name }}!"
template.render({"name": "World"})
# 'Hello World!'
"""
return "".join(self.render_iter(context or {}))
class FileTemplate(Template):
"""
Class that loads a template from a file and allows to rendering it with different contexts.
"""
def __init__(self, template_path: str, *, language: str = Language.HTML) -> None:
"""
Loads a file and creates a reusable template from its contents.
For better performance, instantiate the template in global scope and reuse it as many times.
If memory is a concern, instantiate the template in a function or method that uses it.
By default, the template is rendered as HTML. To render it as XML or Markdown, use the
``language`` parameter.
:param str template_path: Path to a file containing the template to be rendered
:param str language: Language for autoescaping. Defaults to HTML
"""
with open(template_path, "rt", encoding="utf-8") as template_file:
template_string = template_file.read()
super().__init__(template_string, language=language)
def render_string_iter(
template_string: str,
context: dict = None,
*,
chunk_size: int = None,
language: str = Language.HTML,
):
"""
Creates a `Template` from the given ``template_string`` and renders it using the provided
``context``. Returns a generator that yields the rendered output.
:param dict context: Dictionary containing the context for the template
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
the template in chunks sized specifically for the given template
:param str language: Language for autoescaping. Defaults to HTML
Example::
list(render_string_iter(r"Hello {{ name }}!", {"name": "World"}))
# ['Hello ', 'World', '!']
list(render_string_iter(r"Hello {{ name }}!", {"name": "CircuitPython"}, chunk_size=3))
# ['Hel', 'lo ', 'Cir', 'cui', 'tPy', 'tho', 'n!']
"""
return Template(template_string, language=language).render_iter(
context or {}, chunk_size=chunk_size
)
def render_string(
template_string: str,
context: dict = None,
*,
language: str = Language.HTML,
):
"""
Creates a `Template` from the given ``template_string`` and renders it using the provided
``context``. Returns the rendered output as a string.
:param dict context: Dictionary containing the context for the template
:param str language: Language for autoescaping. Defaults to HTML
Example::
render_string(r"Hello {{ name }}!", {"name": "World"})
# 'Hello World!'
"""
return Template(template_string, language=language).render(context or {})
def render_template_iter(
template_path: str,
context: dict = None,
*,
chunk_size: int = None,
language: str = Language.HTML,
):
"""
Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
``context``. Returns a generator that yields the rendered output.
:param dict context: Dictionary containing the context for the template
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
the template in chunks sized specifically for the given template
:param str language: Language for autoescaping. Defaults to HTML
Example::
list(render_template_iter(..., {"name": "World"})) # r"Hello {{ name }}!"
# ['Hello ', 'World', '!']
list(render_template_iter(..., {"name": "CircuitPython"}, chunk_size=3))
# ['Hel', 'lo ', 'Cir', 'cui', 'tPy', 'tho', 'n!']
"""
return FileTemplate(template_path, language=language).render_iter(
context or {}, chunk_size=chunk_size
)
def render_template(
template_path: str,
context: dict = None,
*,
language: str = Language.HTML,
):
"""
Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
``context``. Returns the rendered output as a string.
:param dict context: Dictionary containing the context for the template
:param str language: Language for autoescaping. Defaults to HTML
Example::
render_template(..., {"name": "World"}) # r"Hello {{ name }}!"
# 'Hello World!'
"""
return FileTemplate(template_path, language=language).render(context or {})