support call template_filter without parens (#5736)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 85c5d93, edebd37 · Browse files · 6 files·+409 added−111 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+409 added−111 removed
- tests:+162 added−0 removed
- other:+247 added−111 removed
Provenance
- Commit
ed1c9e9- Tree
f054466- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
85c5d93- Parent 2
edebd37
6 files changed+409 added−111 removed
modified:docs/templating.rst+43 added-17 removed
135 unmodified lines136 using in this block.137 138 .. _registering-filters:139 140-Registering Filters141--------------------140+Registering Filters, Tests, and Globals141+---------------------------------------142 143-If you want to register your own filters in Jinja2 you have two ways to do144-that. You can either put them by hand into the145-:attr:`~flask.Flask.jinja_env` of the application or use the146-:meth:`~flask.Flask.template_filter` decorator.143+The Flask app and blueprints provide decorators and methods to register your own144+filters, tests, and global functions for use in Jinja templates. They all follow145+the same pattern, so the following examples only discuss filters.146 148-The two following examples work the same and both reverse an object::147+Decorate a function with :meth:`~.Flask.template_filter` to register it as a148+template filter.149 150+.. code-block:: python151+152+ @app.template_filter153+ def reverse(s):154+ return reversed(s)155+156+.. code-block:: jinja157+158+ {% for item in data | reverse %}159+ {% endfor %}160+161+By default it will use the name of the function as the name of the filter, but162+that can be changed by passing a name to the decorator.163+164+.. code-block:: python165+150- @app.template_filter('reverse')166+ @app.template_filter("reverse")167 def reverse_filter(s):152- return s[::-1]168+ return reversed(s)169 170+A filter can be registered separately using :meth:`~.Flask.add_template_filter`.171+The name is optional and will use the function name if not given.172+173+.. code-block:: python174+175 def reverse_filter(s):155- return s[::-1]156- app.jinja_env.filters['reverse'] = reverse_filter176+ return reversed(s)177 158-In case of the decorator the argument is optional if you want to use the159-function name as name of the filter. Once registered, you can use the filter160-in your templates in the same way as Jinja2's builtin filters, for example if161-you have a Python list in context called `mylist`::178+ app.add_template_filter(reverse_filter, "reverse")179 163- {% for x in mylist | reverse %}164- {% endfor %}180+For template tests, use the :meth:`~.Flask.template_test` decorator or181+:meth:`~.Flask.add_template_test` method. For template global functions, use the182+:meth:`~.Flask.template_global` decorator or :meth:`~.Flask.add_template_global`183+method.184 185+The same methods also exist on :class:`.Blueprint`, prefixed with ``app_`` to186+indicate that the registered functions will be avaialble to all templates, not187+only when rendering from within the blueprint.188 189+The Jinja environment is also available as :attr:`~.Flask.jinja_env`. It may be190+modified directly, as you would when using Jinja outside Flask.191+192+193 Context Processors194 ------------------195 196 To inject new variables automatically into the context of a template,59 unmodified linesmodified:src/flask/sansio/app.py+96 added-48 removed
658 unmodified lines659 f" endpoint function: {endpoint}"660 )661 self.view_functions[endpoint] = view_func662 663- @setupmethod663+ @t.overload664+ def template_filter(self, name: T_template_filter) -> T_template_filter: ...665+ @t.overload666 def template_filter(667 self, name: str | None = None666- ) -> t.Callable[[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 function669- name will be used. Example::668+ ) -> t.Callable[[T_template_filter], T_template_filter]: ...669+ @setupmethod670+ def template_filter(671+ self, name: T_template_filter | str | None = None672+ ) -> T_template_filter | t.Callable[[T_template_filter], T_template_filter]:673+ """Decorate a function to register it as a custom Jinja filter. The name674+ is optional. The decorator may be used without parentheses.675 671- @app.template_filter()672- def reverse(s):673- return s[::-1]676+ .. code-block:: python677 675- :param name: the optional name of the filter, otherwise the676- function name will be used.678+ @app.template_filter("reverse")679+ def reverse_filter(s):680+ return reversed(s)681+682+ The :meth:`add_template_filter` method may be used to register a683+ function later rather than decorating.684+685+ :param name: The name to register the filter as. If not given, uses the686+ function's name.687 """688+ if callable(name):689+ self.add_template_filter(name)690+ return name691 692 def decorator(f: T_template_filter) -> T_template_filter:693 self.add_template_filter(f, name=name)694 return f3 unmodified lines698 @setupmethod699 def add_template_filter(700 self, f: ft.TemplateFilterCallable, name: str | None = None701 ) -> None:689- """Register a custom template filter. Works exactly like the690- :meth:`template_filter` decorator.702+ """Register a function to use as a custom Jinja filter.703 692- :param name: the optional name of the filter, otherwise the693- function name will be used.704+ The :meth:`template_filter` decorator can be used to register a function705+ by decorating instead.706+707+ :param f: The function to register.708+ :param name: The name to register the filter as. If not given, uses the709+ function's name.710 """711 self.jinja_env.filters[name or f.__name__] = f712 697- @setupmethod713+ @t.overload714+ def template_test(self, name: T_template_test) -> T_template_test: ...715+ @t.overload716 def template_test(717 self, name: str | None = None700- ) -> t.Callable[[T_template_test], T_template_test]:701- """A decorator that is used to register custom template test.702- You can specify a name for the test, otherwise the function703- name will be used. Example::718+ ) -> t.Callable[[T_template_test], T_template_test]: ...719+ @setupmethod720+ def template_test(721+ self, name: T_template_test | str | None = None722+ ) -> T_template_test | t.Callable[[T_template_test], T_template_test]:723+ """Decorate a function to register it as a custom Jinja test. The name724+ is optional. The decorator may be used without parentheses.725 726+ .. code-block:: python727+705- @app.template_test()706- def is_prime(n):707- if n == 2:708- return True709- for i in range(2, int(math.ceil(math.sqrt(n))) + 1):710- if n % i == 0:711- return False728+ @app.template_test("prime")729+ def is_prime_test(n):730+ if n == 2:731+ return True732+ for i in range(2, int(math.ceil(math.sqrt(n))) + 1):733+ if n % i == 0:734+ return False735 return True736 714- .. versionadded:: 0.10737+ The :meth:`add_template_test` method may be used to register a function738+ later rather than decorating.739 716- :param name: the optional name of the test, otherwise the717- function name will be used.740+ :param name: The name to register the filter as. If not given, uses the741+ function's name.742+743+ .. versionadded:: 0.10744 """745+ if callable(name):746+ self.add_template_test(name)747+ return name # type: ignore[return-value]748 749 def decorator(f: T_template_test) -> T_template_test:750 self.add_template_test(f, name=name)751 return f3 unmodified lines755 @setupmethod756 def add_template_test(757 self, f: ft.TemplateTestCallable, name: str | None = None758 ) -> None:730- """Register a custom template test. Works exactly like the731- :meth:`template_test` decorator.759+ """Register a function to use as a custom Jinja test.760 733- .. versionadded:: 0.10761+ The :meth:`template_test` decorator can be used to register a function762+ by decorating instead.763 735- :param name: the optional name of the test, otherwise the736- function name will be used.764+ :param f: The function to register.765+ :param name: The name to register the test as. If not given, uses the766+ function's name.767+768+ .. versionadded:: 0.10769 """770 self.jinja_env.tests[name or f.__name__] = f771 740- @setupmethod772+ @t.overload773+ def template_global(self, name: T_template_global) -> T_template_global: ...774+ @t.overload775 def template_global(776 self, name: str | None = None743- ) -> t.Callable[[T_template_global], T_template_global]:744- """A decorator that is used to register a custom template global function.745- You can specify a name for the global function, otherwise the function746- name will be used. Example::777+ ) -> t.Callable[[T_template_global], T_template_global]: ...778+ @setupmethod779+ def template_global(780+ self, name: T_template_global | str | None = None781+ ) -> T_template_global | t.Callable[[T_template_global], T_template_global]:782+ """Decorate a function to register it as a custom Jinja global. The name783+ is optional. The decorator may be used without parentheses.784 785+ .. code-block:: python786+748- @app.template_global()787+ @app.template_global788 def double(n):789 return 2 * n790 752- .. versionadded:: 0.10791+ The :meth:`add_template_global` method may be used to register a792+ function later rather than decorating.793 754- :param name: the optional name of the global function, otherwise the755- function name will be used.794+ :param name: The name to register the global as. If not given, uses the795+ function's name.796+797+ .. versionadded:: 0.10798 """799+ if callable(name):800+ self.add_template_global(name)801+ return name802 803 def decorator(f: T_template_global) -> T_template_global:804 self.add_template_global(f, name=name)805 return f3 unmodified lines809 @setupmethod810 def add_template_global(811 self, f: ft.TemplateGlobalCallable, name: str | None = None812 ) -> None:768- """Register a custom template global function. Works exactly like the769- :meth:`template_global` decorator.813+ """Register a function to use as a custom Jinja global.814 771- .. versionadded:: 0.10815+ The :meth:`template_global` decorator can be used to register a function816+ by decorating instead.817 773- :param name: the optional name of the global function, otherwise the774- 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 the820+ function's name.821+822+ .. versionadded:: 0.10823 """824 self.jinja_env.globals[name or f.__name__] = f825 826 @setupmethod186 unmodified linesmodified:src/flask/sansio/blueprints.py+106 added-46 removed
438 unmodified lines439 **options,440 )441 )442 443- @setupmethod443+ @t.overload444+ def app_template_filter(self, name: T_template_filter) -> T_template_filter: ...445+ @t.overload446 def app_template_filter(447 self, name: str | None = None446- ) -> t.Callable[[T_template_filter], T_template_filter]:447- """Register a template filter, available in any template rendered by the448- application. Equivalent to :meth:`.Flask.template_filter`.448+ ) -> t.Callable[[T_template_filter], T_template_filter]: ...449+ @setupmethod450+ def app_template_filter(451+ self, name: T_template_filter | str | None = None452+ ) -> T_template_filter | t.Callable[[T_template_filter], T_template_filter]:453+ """Decorate a function to register it as a custom Jinja filter. The name454+ is optional. The decorator may be used without parentheses.455 450- :param name: the optional name of the filter, otherwise the451- function name will be used.456+ The :meth:`add_app_template_filter` method may be used to register a457+ function later rather than decorating.458+459+ The filter is available in all templates, not only those under this460+ blueprint. Equivalent to :meth:`.Flask.template_filter`.461+462+ :param name: The name to register the filter as. If not given, uses the463+ function's name.464 """465+ if callable(name):466+ self.add_app_template_filter(name)467+ return name468 469 def decorator(f: T_template_filter) -> T_template_filter:470 self.add_app_template_filter(f, name=name)471 return f3 unmodified lines475 @setupmethod476 def add_app_template_filter(477 self, f: ft.TemplateFilterCallable, name: str | None = None478 ) -> None:464- """Register a template filter, available in any template rendered by the465- application. Works like the :meth:`app_template_filter` decorator. Equivalent to466- :meth:`.Flask.add_template_filter`.479+ """Register a function to use as a custom Jinja filter.480 468- :param name: the optional name of the filter, otherwise the469- function name will be used.481+ The :meth:`app_template_filter` decorator can be used to register a482+ function by decorating instead.483+484+ The filter is available in all templates, not only those under this485+ blueprint. Equivalent to :meth:`.Flask.add_template_filter`.486+487+ :param f: The function to register.488+ :param name: The name to register the filter as. If not given, uses the489+ function's name.490 """491 472- def register_template(state: BlueprintSetupState) -> None:473- state.app.jinja_env.filters[name or f.__name__] = f492+ def register_template_filter(state: BlueprintSetupState) -> None:493+ state.app.add_template_filter(f, name=name)494 475- self.record_once(register_template)495+ self.record_once(register_template_filter)496 477- @setupmethod497+ @t.overload498+ def app_template_test(self, name: T_template_test) -> T_template_test: ...499+ @t.overload500 def app_template_test(501 self, name: str | None = None480- ) -> t.Callable[[T_template_test], T_template_test]:481- """Register a template test, available in any template rendered by the482- application. Equivalent to :meth:`.Flask.template_test`.502+ ) -> t.Callable[[T_template_test], T_template_test]: ...503+ @setupmethod504+ def app_template_test(505+ self, name: T_template_test | str | None = None506+ ) -> T_template_test | t.Callable[[T_template_test], T_template_test]:507+ """Decorate a function to register it as a custom Jinja test. The name508+ is optional. The decorator may be used without parentheses.509 484- .. versionadded:: 0.10510+ The :meth:`add_app_template_test` method may be used to register a511+ function later rather than decorating.512 486- :param name: the optional name of the test, otherwise the487- function name will be used.513+ The test is available in all templates, not only those under this514+ blueprint. Equivalent to :meth:`.Flask.template_test`.515+516+ :param name: The name to register the filter as. If not given, uses the517+ function's name.518+519+ .. versionadded:: 0.10520 """521+ if callable(name):522+ self.add_app_template_test(name)523+ return name # type: ignore[return-value]524 525 def decorator(f: T_template_test) -> T_template_test:526 self.add_app_template_test(f, name=name)527 return f3 unmodified lines531 @setupmethod532 def add_app_template_test(533 self, f: ft.TemplateTestCallable, name: str | None = None534 ) -> None:500- """Register a template test, available in any template rendered by the501- application. Works like the :meth:`app_template_test` decorator. Equivalent to502- :meth:`.Flask.add_template_test`.535+ """Register a function to use as a custom Jinja test.536 504- .. versionadded:: 0.10537+ The :meth:`app_template_test` decorator can be used to register a538+ function by decorating instead.539 506- :param name: the optional name of the test, otherwise the507- function name will be used.540+ The test is available in all templates, not only those under this541+ blueprint. Equivalent to :meth:`.Flask.add_template_test`.542+543+ :param f: The function to register.544+ :param name: The name to register the test as. If not given, uses the545+ function's name.546+547+ .. versionadded:: 0.10548 """549 510- def register_template(state: BlueprintSetupState) -> None:511- state.app.jinja_env.tests[name or f.__name__] = f550+ def register_template_test(state: BlueprintSetupState) -> None:551+ state.app.add_template_test(f, name=name)552 513- self.record_once(register_template)553+ self.record_once(register_template_test)554 515- @setupmethod555+ @t.overload556+ def app_template_global(self, name: T_template_global) -> T_template_global: ...557+ @t.overload558 def app_template_global(559 self, name: str | None = None518- ) -> t.Callable[[T_template_global], T_template_global]:519- """Register a template global, available in any template rendered by the520- application. Equivalent to :meth:`.Flask.template_global`.560+ ) -> t.Callable[[T_template_global], T_template_global]: ...561+ @setupmethod562+ def app_template_global(563+ self, name: T_template_global | str | None = None564+ ) -> T_template_global | t.Callable[[T_template_global], T_template_global]:565+ """Decorate a function to register it as a custom Jinja global. The name566+ is optional. The decorator may be used without parentheses.567 522- .. versionadded:: 0.10568+ The :meth:`add_app_template_global` method may be used to register a569+ function later rather than decorating.570 524- :param name: the optional name of the global, otherwise the525- function name will be used.571+ The global is available in all templates, not only those under this572+ blueprint. Equivalent to :meth:`.Flask.template_global`.573+574+ :param name: The name to register the global as. If not given, uses the575+ function's name.576+577+ .. versionadded:: 0.10578 """579+ if callable(name):580+ self.add_app_template_global(name)581+ return name582 583 def decorator(f: T_template_global) -> T_template_global:584 self.add_app_template_global(f, name=name)585 return f3 unmodified lines589 @setupmethod590 def add_app_template_global(591 self, f: ft.TemplateGlobalCallable, name: str | None = None592 ) -> None:538- """Register a template global, available in any template rendered by the539- application. Works like the :meth:`app_template_global` decorator. Equivalent to540- :meth:`.Flask.add_template_global`.593+ """Register a function to use as a custom Jinja global.594 542- .. versionadded:: 0.10595+ The :meth:`app_template_global` decorator can be used to register a function596+ by decorating instead.597 544- :param name: the optional name of the global, otherwise the545- function name will be used.598+ The global is available in all templates, not only those under this599+ blueprint. Equivalent to :meth:`.Flask.add_template_global`.600+601+ :param f: The function to register.602+ :param name: The name to register the global as. If not given, uses the603+ function's name.604+605+ .. versionadded:: 0.10606 """607 548- def register_template(state: BlueprintSetupState) -> None:549- state.app.jinja_env.globals[name or f.__name__] = f608+ def register_template_global(state: BlueprintSetupState) -> None:609+ state.app.add_template_global(f, name=name)610 551- self.record_once(register_template)611+ self.record_once(register_template_global)612 613 @setupmethod614 def before_app_request(self, f: T_before_request) -> T_before_request:615 """Like :meth:`before_request`, but before every request, not only those handled77 unmodified linesmodified:tests/test_blueprints.py+81 added
364 unmodified lines365 @bp.app_template_filter()366 def my_reverse(s):367 return s[::-1]368 369+ @bp.app_template_filter370+ def my_reverse_2(s):371+ return s[::-1]372+373+ @bp.app_template_filter("my_reverse_custom_name_3")374+ def my_reverse_3(s):375+ return s[::-1]376+377+ @bp.app_template_filter(name="my_reverse_custom_name_4")378+ def my_reverse_4(s):379+ return s[::-1]380+381 app.register_blueprint(bp, url_prefix="/py")382 assert "my_reverse" in app.jinja_env.filters.keys()383 assert app.jinja_env.filters["my_reverse"] == my_reverse384 assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"385 386+ assert "my_reverse_2" in app.jinja_env.filters.keys()387+ assert app.jinja_env.filters["my_reverse_2"] == my_reverse_2388+ assert app.jinja_env.filters["my_reverse_2"]("abcd") == "dcba"389 390+ assert "my_reverse_custom_name_3" in app.jinja_env.filters.keys()391+ assert app.jinja_env.filters["my_reverse_custom_name_3"] == my_reverse_3392+ assert app.jinja_env.filters["my_reverse_custom_name_3"]("abcd") == "dcba"393+394+ assert "my_reverse_custom_name_4" in app.jinja_env.filters.keys()395+ assert app.jinja_env.filters["my_reverse_custom_name_4"] == my_reverse_4396+ assert app.jinja_env.filters["my_reverse_custom_name_4"]("abcd") == "dcba"397+398+399 def test_add_template_filter(app):400 bp = flask.Blueprint("bp", __name__)401 402 def my_reverse(s):122 unmodified lines525 @bp.app_template_test()526 def is_boolean(value):527 return isinstance(value, bool)528 529+ @bp.app_template_test530+ def boolean_2(value):531+ return isinstance(value, bool)532+533+ @bp.app_template_test("my_boolean_custom_name")534+ def boolean_3(value):535+ return isinstance(value, bool)536+537+ @bp.app_template_test(name="my_boolean_custom_name_2")538+ def boolean_4(value):539+ return isinstance(value, bool)540+541 app.register_blueprint(bp, url_prefix="/py")542 assert "is_boolean" in app.jinja_env.tests.keys()543 assert app.jinja_env.tests["is_boolean"] == is_boolean544 assert app.jinja_env.tests["is_boolean"](False)545 546+ assert "boolean_2" in app.jinja_env.tests.keys()547+ assert app.jinja_env.tests["boolean_2"] == boolean_2548+ assert app.jinja_env.tests["boolean_2"](False)549 550+ assert "my_boolean_custom_name" in app.jinja_env.tests.keys()551+ assert app.jinja_env.tests["my_boolean_custom_name"] == boolean_3552+ assert app.jinja_env.tests["my_boolean_custom_name"](False)553+554+ assert "my_boolean_custom_name_2" in app.jinja_env.tests.keys()555+ assert app.jinja_env.tests["my_boolean_custom_name_2"] == boolean_4556+ assert app.jinja_env.tests["my_boolean_custom_name_2"](False)557+558+559 def test_add_template_test(app):560 bp = flask.Blueprint("bp", __name__)561 562 def is_boolean(value):163 unmodified lines726 @bp.app_template_global()727 def get_answer():728 return 42729 730+ @bp.app_template_global731+ def get_stuff_1():732+ return "get_stuff_1"733+734+ @bp.app_template_global("my_get_stuff_custom_name_2")735+ def get_stuff_2():736+ return "get_stuff_2"737+738+ @bp.app_template_global(name="my_get_stuff_custom_name_3")739+ def get_stuff_3():740+ return "get_stuff_3"741+742 # Make sure the function is not in the jinja_env already743 assert "get_answer" not in app.jinja_env.globals.keys()744 app.register_blueprint(bp)745 1 unmodified line747 assert "get_answer" in app.jinja_env.globals.keys()748 assert app.jinja_env.globals["get_answer"] is get_answer749 assert app.jinja_env.globals["get_answer"]() == 42750 751+ assert "get_stuff_1" in app.jinja_env.globals.keys()752+ assert app.jinja_env.globals["get_stuff_1"] == get_stuff_1753+ assert app.jinja_env.globals["get_stuff_1"](), "get_stuff_1"754+755+ assert "my_get_stuff_custom_name_2" in app.jinja_env.globals.keys()756+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"] == get_stuff_2757+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"](), "get_stuff_2"758+759+ assert "my_get_stuff_custom_name_3" in app.jinja_env.globals.keys()760+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"] == get_stuff_3761+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"](), "get_stuff_3"762+763 with app.app_context():764 rv = flask.render_template_string("{{ get_answer() }}")765 assert rv == "42"766 767+ rv = flask.render_template_string("{{ get_stuff_1() }}")768+ assert rv == "get_stuff_1"769 770+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")771+ assert rv == "get_stuff_2"772+773+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")774+ assert rv == "get_stuff_3"775+776+777 def test_request_processing(app, client):778 bp = flask.Blueprint("bp", __name__)779 evts = []780 347 unmodified linesmodified:tests/test_templating.py+81 added
127 unmodified lines128 assert "my_reverse" in app.jinja_env.filters.keys()129 assert app.jinja_env.filters["my_reverse"] == my_reverse130 assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"131 132+ @app.template_filter133+ def my_reverse_2(s):134+ return s[::-1]135 136+ assert "my_reverse_2" in app.jinja_env.filters.keys()137+ assert app.jinja_env.filters["my_reverse_2"] == my_reverse_2138+ assert app.jinja_env.filters["my_reverse_2"]("abcd") == "dcba"139+140+ @app.template_filter("my_reverse_custom_name_3")141+ def my_reverse_3(s):142+ return s[::-1]143+144+ assert "my_reverse_custom_name_3" in app.jinja_env.filters.keys()145+ assert app.jinja_env.filters["my_reverse_custom_name_3"] == my_reverse_3146+ assert app.jinja_env.filters["my_reverse_custom_name_3"]("abcd") == "dcba"147+148+ @app.template_filter(name="my_reverse_custom_name_4")149+ def my_reverse_4(s):150+ return s[::-1]151+152+ assert "my_reverse_custom_name_4" in app.jinja_env.filters.keys()153+ assert app.jinja_env.filters["my_reverse_custom_name_4"] == my_reverse_4154+ assert app.jinja_env.filters["my_reverse_custom_name_4"]("abcd") == "dcba"155+156+157 def test_add_template_filter(app):158 def my_reverse(s):159 return s[::-1]160 85 unmodified lines246 assert "boolean" in app.jinja_env.tests.keys()247 assert app.jinja_env.tests["boolean"] == boolean248 assert app.jinja_env.tests["boolean"](False)249 250+ @app.template_test251+ def boolean_2(value):252+ return isinstance(value, bool)253 254+ assert "boolean_2" in app.jinja_env.tests.keys()255+ assert app.jinja_env.tests["boolean_2"] == boolean_2256+ assert app.jinja_env.tests["boolean_2"](False)257+258+ @app.template_test("my_boolean_custom_name")259+ def boolean_3(value):260+ return isinstance(value, bool)261+262+ assert "my_boolean_custom_name" in app.jinja_env.tests.keys()263+ assert app.jinja_env.tests["my_boolean_custom_name"] == boolean_3264+ assert app.jinja_env.tests["my_boolean_custom_name"](False)265+266+ @app.template_test(name="my_boolean_custom_name_2")267+ def boolean_4(value):268+ return isinstance(value, bool)269+270+ assert "my_boolean_custom_name_2" in app.jinja_env.tests.keys()271+ assert app.jinja_env.tests["my_boolean_custom_name_2"] == boolean_4272+ assert app.jinja_env.tests["my_boolean_custom_name_2"](False)273+274+275 def test_add_template_test(app):276 def boolean(value):277 return isinstance(value, bool)278 88 unmodified lines367 368 rv = flask.render_template_string("{{ get_stuff() }}")369 assert rv == "42"370 371+ @app.template_global372+ def get_stuff_1():373+ return "get_stuff_1"374 375+ assert "get_stuff_1" in app.jinja_env.globals.keys()376+ assert app.jinja_env.globals["get_stuff_1"] == get_stuff_1377+ assert app.jinja_env.globals["get_stuff_1"](), "get_stuff_1"378+379+ rv = flask.render_template_string("{{ get_stuff_1() }}")380+ assert rv == "get_stuff_1"381+382+ @app.template_global("my_get_stuff_custom_name_2")383+ def get_stuff_2():384+ return "get_stuff_2"385+386+ assert "my_get_stuff_custom_name_2" in app.jinja_env.globals.keys()387+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"] == get_stuff_2388+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"](), "get_stuff_2"389+390+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")391+ assert rv == "get_stuff_2"392+393+ @app.template_global(name="my_get_stuff_custom_name_3")394+ def get_stuff_3():395+ return "get_stuff_3"396+397+ assert "my_get_stuff_custom_name_3" in app.jinja_env.globals.keys()398+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"] == get_stuff_3399+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"](), "get_stuff_3"400+401+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")402+ assert rv == "get_stuff_3"403+404+405 def test_custom_template_loader(client):406 class MyFlask(flask.Flask):407 def create_global_jinja_loader(self):408 from jinja2 import DictLoader124 unmodified linesmodified:CHANGES.rst+2 added
3 unmodified lines4 Unreleased5 6 - Drop support for Python 3.9. :pr:`5730`7 - Remove previously deprecated code: ``__version__``. :pr:`5648`8+- ``template_filter``, ``template_test``, and ``template_global`` decorators9+ can be used without parentheses. :issue:`5729`10 11 12 Version 3.1.113 -------------1611 unmodified lines