@@ -848,12 +848,16 @@ def __init__(self, template_path: str, *, language: str = Language.HTML) -> None
848
848
super ().__init__ (template_string , language = language )
849
849
850
850
851
+ _CACHE : "dict[int, Template| FileTemplate]" = {}
852
+
853
+
851
854
def render_string_iter (
852
855
template_string : str ,
853
856
context : dict = None ,
854
857
* ,
855
858
chunk_size : int = None ,
856
859
language : str = Language .HTML ,
860
+ cache : bool = True ,
857
861
):
858
862
"""
859
863
Creates a `Template` from the given ``template_string`` and renders it using the provided
@@ -863,6 +867,7 @@ def render_string_iter(
863
867
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
864
868
the template in chunks sized specifically for the given template
865
869
:param str language: Language for autoescaping. Defaults to HTML
870
+ :param bool cache: When ``True``, the template is saved and reused on next calls.
866
871
867
872
Example::
868
873
@@ -872,8 +877,20 @@ def render_string_iter(
872
877
list(render_string_iter(r"Hello {{ name }}!", {"name": "CircuitPython"}, chunk_size=3))
873
878
# ['Hel', 'lo ', 'Cir', 'cui', 'tPy', 'tho', 'n!']
874
879
"""
875
- return Template (template_string , language = language ).render_iter (
876
- context or {}, chunk_size = chunk_size
880
+ key = hash (template_string )
881
+
882
+ if cache and key in _CACHE :
883
+ return _yield_as_sized_chunks (
884
+ _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
885
+ )
886
+
887
+ template = Template (template_string , language = language )
888
+
889
+ if cache :
890
+ _CACHE [key ] = template
891
+
892
+ return _yield_as_sized_chunks (
893
+ template .render_iter (context or {}), chunk_size = chunk_size
877
894
)
878
895
879
896
@@ -882,20 +899,32 @@ def render_string(
882
899
context : dict = None ,
883
900
* ,
884
901
language : str = Language .HTML ,
902
+ cache : bool = True ,
885
903
):
886
904
"""
887
905
Creates a `Template` from the given ``template_string`` and renders it using the provided
888
906
``context``. Returns the rendered output as a string.
889
907
890
908
:param dict context: Dictionary containing the context for the template
891
909
:param str language: Language for autoescaping. Defaults to HTML
910
+ :param bool cache: When ``True``, the template is saved and reused on next calls.
892
911
893
912
Example::
894
913
895
914
render_string(r"Hello {{ name }}!", {"name": "World"})
896
915
# 'Hello World!'
897
916
"""
898
- return Template (template_string , language = language ).render (context or {})
917
+ key = hash (template_string )
918
+
919
+ if cache and key in _CACHE :
920
+ return _CACHE [key ].render (context or {})
921
+
922
+ template = Template (template_string , language = language )
923
+
924
+ if cache :
925
+ _CACHE [key ] = template
926
+
927
+ return template .render (context or {})
899
928
900
929
901
930
def render_template_iter (
@@ -904,6 +933,7 @@ def render_template_iter(
904
933
* ,
905
934
chunk_size : int = None ,
906
935
language : str = Language .HTML ,
936
+ cache : bool = True ,
907
937
):
908
938
"""
909
939
Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
@@ -913,6 +943,7 @@ def render_template_iter(
913
943
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
914
944
the template in chunks sized specifically for the given template
915
945
:param str language: Language for autoescaping. Defaults to HTML
946
+ :param bool cache: When ``True``, the template is saved and reused on next calls.
916
947
917
948
Example::
918
949
@@ -922,8 +953,20 @@ def render_template_iter(
922
953
list(render_template_iter(..., {"name": "CircuitPython"}, chunk_size=3))
923
954
# ['Hel', 'lo ', 'Cir', 'cui', 'tPy', 'tho', 'n!']
924
955
"""
925
- return FileTemplate (template_path , language = language ).render_iter (
926
- context or {}, chunk_size = chunk_size
956
+ key = hash (template_path )
957
+
958
+ if cache and key in _CACHE :
959
+ return _yield_as_sized_chunks (
960
+ _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
961
+ )
962
+
963
+ template = FileTemplate (template_path , language = language )
964
+
965
+ if cache :
966
+ _CACHE [key ] = template
967
+
968
+ return _yield_as_sized_chunks (
969
+ template .render_iter (context or {}, chunk_size = chunk_size ), chunk_size
927
970
)
928
971
929
972
@@ -932,17 +975,30 @@ def render_template(
932
975
context : dict = None ,
933
976
* ,
934
977
language : str = Language .HTML ,
978
+ cache : bool = True ,
935
979
):
936
980
"""
937
981
Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
938
982
``context``. Returns the rendered output as a string.
939
983
940
984
:param dict context: Dictionary containing the context for the template
941
985
:param str language: Language for autoescaping. Defaults to HTML
986
+ :param bool cache: When ``True``, the template is saved and reused on next calls.
942
987
943
988
Example::
944
989
945
990
render_template(..., {"name": "World"}) # r"Hello {{ name }}!"
946
991
# 'Hello World!'
947
992
"""
948
- return FileTemplate (template_path , language = language ).render (context or {})
993
+
994
+ key = hash (template_path )
995
+
996
+ if cache and key in _CACHE :
997
+ return _CACHE [key ].render (context or {})
998
+
999
+ template = FileTemplate (template_path , language = language )
1000
+
1001
+ if cache :
1002
+ _CACHE [key ] = template
1003
+
1004
+ return template .render (context or {})
0 commit comments