@@ -660,37 +660,34 @@ def add_url_rule(
660
660
)
661
661
self .view_functions [endpoint ] = view_func
662
662
663
+ @t .overload
664
+ def template_filter (self , name : T_template_filter ) -> T_template_filter : ...
665
+ @t .overload
666
+ def template_filter (
667
+ self , name : str | None = None
668
+ ) -> t .Callable [[T_template_filter ], T_template_filter ]: ...
663
669
@setupmethod
664
670
def template_filter (
665
- self , name : t .Callable [..., t .Any ] | str | None = None
666
- ) -> t .Callable [[T_template_filter ], T_template_filter ] | T_template_filter :
667
- """A decorator that is used to register custom template filter.
668
- You can specify a name for the filter, otherwise the function
669
- name will be used. Example::
671
+ self , name : T_template_filter | str | None = None
672
+ ) -> T_template_filter | t .Callable [[T_template_filter ], T_template_filter ]:
673
+ """Decorate a function to register it as a custom Jinja filter. The name
674
+ is optional. The decorator may be used without parentheses.
670
675
671
- @app.template_filter()
672
- def reverse(s):
673
- return s[::-1]
676
+ .. code-block:: python
674
677
675
- The decorator also can be used without parentheses::
678
+ @app.template_filter("reverse")
679
+ def reverse_filter(s):
680
+ return reversed(s)
676
681
677
- @app.template_filter
678
- def reverse(s):
679
- return s[::-1]
682
+ The :meth:`add_template_filter` method may be used to register a
683
+ function later rather than decorating.
680
684
681
- :param name: the optional name of the filter, otherwise the
682
- function name will be used .
685
+ :param name: The name to register the filter as. If not given, uses the
686
+ function's name.
683
687
"""
684
-
685
688
if callable (name ):
686
- # If name is callable, it is the function to register.
687
- # This is a shortcut for the common case of
688
- # @app.template_filter
689
- # def func():
690
-
691
- func = name
692
- self .add_template_filter (func )
693
- return func
689
+ self .add_template_filter (name )
690
+ return name
694
691
695
692
def decorator (f : T_template_filter ) -> T_template_filter :
696
693
self .add_template_filter (f , name = name )
@@ -702,56 +699,52 @@ def decorator(f: T_template_filter) -> T_template_filter:
702
699
def add_template_filter (
703
700
self , f : ft .TemplateFilterCallable , name : str | None = None
704
701
) -> None :
705
- """Register a custom template filter. Works exactly like the
706
- :meth:`template_filter` decorator.
702
+ """Register a function to use as a custom Jinja filter.
703
+
704
+ The :meth:`template_filter` decorator can be used to register a function
705
+ by decorating instead.
707
706
708
- :param name: the optional name of the filter, otherwise the
709
- function name will be used.
707
+ :param f: The function to register.
708
+ :param name: The name to register the filter as. If not given, uses the
709
+ function's name.
710
710
"""
711
711
self .jinja_env .filters [name or f .__name__ ] = f
712
712
713
+ @t .overload
714
+ def template_test (self , name : T_template_test ) -> T_template_test : ...
715
+ @t .overload
716
+ def template_test (
717
+ self , name : str | None = None
718
+ ) -> t .Callable [[T_template_test ], T_template_test ]: ...
713
719
@setupmethod
714
720
def template_test (
715
- self , name : t .Callable [..., t .Any ] | str | None = None
716
- ) -> t .Callable [[T_template_test ], T_template_test ] | T_template_filter :
717
- """A decorator that is used to register custom template test.
718
- You can specify a name for the test, otherwise the function
719
- name will be used. Example::
720
-
721
- @app.template_test()
722
- def is_prime(n):
721
+ self , name : T_template_test | str | None = None
722
+ ) -> T_template_test | t .Callable [[T_template_test ], T_template_test ]:
723
+ """Decorate a function to register it as a custom Jinja test. The name
724
+ is optional. The decorator may be used without parentheses.
725
+
726
+ .. code-block:: python
727
+
728
+ @app.template_test("prime")
729
+ def is_prime_test(n):
723
730
if n == 2:
724
731
return True
725
732
for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
726
733
if n % i == 0:
727
734
return False
728
735
return True
729
736
730
- The decorator also can be used without parentheses::
737
+ The :meth:`add_template_test` method may be used to register a function
738
+ later rather than decorating.
731
739
732
- @app.template_test
733
- def is_prime(n):
734
- if n == 2:
735
- return True
736
- for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
737
- if n % i == 0:
738
- return False
740
+ :param name: The name to register the filter as. If not given, uses the
741
+ function's name.
739
742
740
743
.. versionadded:: 0.10
741
-
742
- :param name: the optional name of the test, otherwise the
743
- function name will be used.
744
744
"""
745
-
746
745
if callable (name ):
747
- # If name is callable, it is the function to register.
748
- # This is a shortcut for the common case of
749
- # @app.template_test
750
- # def func():
751
-
752
- func = name
753
- self .add_template_test (func )
754
- return func
746
+ self .add_template_test (name )
747
+ return name # type: ignore[return-value]
755
748
756
749
def decorator (f : T_template_test ) -> T_template_test :
757
750
self .add_template_test (f , name = name )
@@ -763,49 +756,49 @@ def decorator(f: T_template_test) -> T_template_test:
763
756
def add_template_test (
764
757
self , f : ft .TemplateTestCallable , name : str | None = None
765
758
) -> None :
766
- """Register a custom template test. Works exactly like the
767
- :meth:`template_test` decorator.
759
+ """Register a function to use as a custom Jinja test.
768
760
769
- .. versionadded:: 0.10
761
+ The :meth:`template_test` decorator can be used to register a function
762
+ by decorating instead.
763
+
764
+ :param f: The function to register.
765
+ :param name: The name to register the test as. If not given, uses the
766
+ function's name.
770
767
771
- :param name: the optional name of the test, otherwise the
772
- function name will be used.
768
+ .. versionadded:: 0.10
773
769
"""
774
770
self .jinja_env .tests [name or f .__name__ ] = f
775
771
772
+ @t .overload
773
+ def template_global (self , name : T_template_global ) -> T_template_global : ...
774
+ @t .overload
775
+ def template_global (
776
+ self , name : str | None = None
777
+ ) -> t .Callable [[T_template_global ], T_template_global ]: ...
776
778
@setupmethod
777
779
def template_global (
778
- self , name : t .Callable [..., t .Any ] | str | None = None
779
- ) -> t .Callable [[T_template_global ], T_template_global ] | T_template_filter :
780
- """A decorator that is used to register a custom template global function.
781
- You can specify a name for the global function, otherwise the function
782
- name will be used. Example::
780
+ self , name : T_template_global | str | None = None
781
+ ) -> T_template_global | t .Callable [[T_template_global ], T_template_global ]:
782
+ """Decorate a function to register it as a custom Jinja global. The name
783
+ is optional. The decorator may be used without parentheses.
783
784
784
- @app.template_global()
785
- def double(n):
786
- return 2 * n
787
-
788
- The decorator also can be used without parentheses::
785
+ .. code-block:: python
789
786
790
787
@app.template_global
791
788
def double(n):
792
789
return 2 * n
793
790
794
- .. versionadded:: 0.10
791
+ The :meth:`add_template_global` method may be used to register a
792
+ function later rather than decorating.
795
793
796
- :param name: the optional name of the global function, otherwise the
797
- function name will be used.
798
- """
794
+ :param name: The name to register the global as. If not given, uses the
795
+ function's name.
799
796
797
+ .. versionadded:: 0.10
798
+ """
800
799
if callable (name ):
801
- # If name is callable, it is the function to register.
802
- # This is a shortcut for the common case of
803
- # @app.template_global
804
- # def func():
805
-
806
- func = name
807
- self .add_template_global (func )
808
- return func
800
+ self .add_template_global (name )
801
+ return name
809
802
810
803
def decorator (f : T_template_global ) -> T_template_global :
811
804
self .add_template_global (f , name = name )
@@ -817,13 +810,16 @@ def decorator(f: T_template_global) -> T_template_global:
817
810
def add_template_global (
818
811
self , f : ft .TemplateGlobalCallable , name : str | None = None
819
812
) -> None :
820
- """Register a custom template global function. Works exactly like the
821
- :meth:`template_global` decorator.
813
+ """Register a function to use as a custom Jinja global.
822
814
823
- .. versionadded:: 0.10
815
+ The :meth:`template_global` decorator can be used to register a function
816
+ by decorating instead.
824
817
825
- :param name: the optional name of the global function, otherwise the
826
- function name will be used.
818
+ :param f: The function to register.
819
+ :param name: The name to register the global as. If not given, uses the
820
+ function's name.
821
+
822
+ .. versionadded:: 0.10
827
823
"""
828
824
self .jinja_env .globals [name or f .__name__ ] = f
829
825
0 commit comments