Skip to content
Myr

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

6 files+409 added−111 removed

modified:docs/templating.rst+43 added-17 removed
135 unmodified lines
136using in this block.137138.. _registering-filters:139140Registering Filters141-------------------140Registering Filters, Tests, and Globals141---------------------------------------142143If you want to register your own filters in Jinja2 you have two ways to do144that. 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.143The Flask app and blueprints provide decorators and methods to register your own144filters, tests, and global functions for use in Jinja templates. They all follow145the same pattern, so the following examples only discuss filters.146148The two following examples work the same and both reverse an object::147Decorate a function with :meth:`~.Flask.template_filter` to register it as a148template filter.149150.. code-block:: python151152 @app.template_filter153 def reverse(s):154 return reversed(s)155156.. code-block:: jinja157158 {% for item in data | reverse %}159 {% endfor %}160161By default it will use the name of the function as the name of the filter, but162that can be changed by passing a name to the decorator.163164.. code-block:: python165150 @app.template_filter('reverse')166 @app.template_filter("reverse")167 def reverse_filter(s):152 return s[::-1]168 return reversed(s)169170A filter can be registered separately using :meth:`~.Flask.add_template_filter`.171The name is optional and will use the function name if not given.172173.. code-block:: python174175 def reverse_filter(s):155 return s[::-1]156 app.jinja_env.filters['reverse'] = reverse_filter176 return reversed(s)177158In case of the decorator the argument is optional if you want to use the159function name as name of the filter. Once registered, you can use the filter160in your templates in the same way as Jinja2's builtin filters, for example if161you have a Python list in context called `mylist`::178 app.add_template_filter(reverse_filter, "reverse")179163 {% for x in mylist | reverse %}164 {% endfor %}180For 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`183method.184185The same methods also exist on :class:`.Blueprint`, prefixed with ``app_`` to186indicate that the registered functions will be avaialble to all templates, not187only when rendering from within the blueprint.188189The Jinja environment is also available as :attr:`~.Flask.jinja_env`. It may be190modified directly, as you would when using Jinja outside Flask.191192193Context Processors194------------------195196To inject new variables automatically into the context of a template,
59 unmodified lines
modified:src/flask/sansio/app.py+96 added-48 removed
658 unmodified lines
659 f" endpoint function: {endpoint}"660 )661 self.view_functions[endpoint] = view_func662663 @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.675671 @app.template_filter()672 def reverse(s):673 return s[::-1]676 .. code-block:: python677675 :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)681682 The :meth:`add_template_filter` method may be used to register a683 function later rather than decorating.684685 :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 name691692 def decorator(f: T_template_filter) -> T_template_filter:693 self.add_template_filter(f, name=name)694 return f
3 unmodified lines
698 @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.703692 :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.706707 :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__] = f712697 @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.725726 .. code-block:: python727705 @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 True736714 .. versionadded:: 0.10737 The :meth:`add_template_test` method may be used to register a function738 later rather than decorating.739716 :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.742743 .. versionadded:: 0.10744 """745 if callable(name):746 self.add_template_test(name)747 return name # type: ignore[return-value]748749 def decorator(f: T_template_test) -> T_template_test:750 self.add_template_test(f, name=name)751 return f
3 unmodified lines
755 @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.760733 .. versionadded:: 0.10761 The :meth:`template_test` decorator can be used to register a function762 by decorating instead.763735 :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.767768 .. versionadded:: 0.10769 """770 self.jinja_env.tests[name or f.__name__] = f771740 @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.784785 .. code-block:: python786748 @app.template_global()787 @app.template_global788 def double(n):789 return 2 * n790752 .. versionadded:: 0.10791 The :meth:`add_template_global` method may be used to register a792 function later rather than decorating.793754 :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.796797 .. versionadded:: 0.10798 """799 if callable(name):800 self.add_template_global(name)801 return name802803 def decorator(f: T_template_global) -> T_template_global:804 self.add_template_global(f, name=name)805 return f
3 unmodified lines
809 @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.814771 .. versionadded:: 0.10815 The :meth:`template_global` decorator can be used to register a function816 by decorating instead.817773 :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.821822 .. versionadded:: 0.10823 """824 self.jinja_env.globals[name or f.__name__] = f825826 @setupmethod
186 unmodified lines
modified:src/flask/sansio/blueprints.py+106 added-46 removed
438 unmodified lines
439 **options,440 )441 )442443 @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.455450 :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.458459 The filter is available in all templates, not only those under this460 blueprint. Equivalent to :meth:`.Flask.template_filter`.461462 :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 name468469 def decorator(f: T_template_filter) -> T_template_filter:470 self.add_app_template_filter(f, name=name)471 return f
3 unmodified lines
475 @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.480468 :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.483484 The filter is available in all templates, not only those under this485 blueprint. Equivalent to :meth:`.Flask.add_template_filter`.486487 :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 """491472 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)494475 self.record_once(register_template)495 self.record_once(register_template_filter)496477 @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.509484 .. versionadded:: 0.10510 The :meth:`add_app_template_test` method may be used to register a511 function later rather than decorating.512486 :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`.515516 :param name: The name to register the filter as. If not given, uses the517 function's name.518519 .. versionadded:: 0.10520 """521 if callable(name):522 self.add_app_template_test(name)523 return name # type: ignore[return-value]524525 def decorator(f: T_template_test) -> T_template_test:526 self.add_app_template_test(f, name=name)527 return f
3 unmodified lines
531 @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.536504 .. versionadded:: 0.10537 The :meth:`app_template_test` decorator can be used to register a538 function by decorating instead.539506 :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`.542543 :param f: The function to register.544 :param name: The name to register the test as. If not given, uses the545 function's name.546547 .. versionadded:: 0.10548 """549510 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)552513 self.record_once(register_template)553 self.record_once(register_template_test)554515 @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.567522 .. versionadded:: 0.10568 The :meth:`add_app_template_global` method may be used to register a569 function later rather than decorating.570524 :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`.573574 :param name: The name to register the global as. If not given, uses the575 function's name.576577 .. versionadded:: 0.10578 """579 if callable(name):580 self.add_app_template_global(name)581 return name582583 def decorator(f: T_template_global) -> T_template_global:584 self.add_app_template_global(f, name=name)585 return f
3 unmodified lines
589 @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.594542 .. versionadded:: 0.10595 The :meth:`app_template_global` decorator can be used to register a function596 by decorating instead.597544 :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`.600601 :param f: The function to register.602 :param name: The name to register the global as. If not given, uses the603 function's name.604605 .. versionadded:: 0.10606 """607548 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)610551 self.record_once(register_template)611 self.record_once(register_template_global)612613 @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 handled
77 unmodified lines
modified:tests/test_blueprints.py+81 added
364 unmodified lines
365 @bp.app_template_filter()366 def my_reverse(s):367 return s[::-1]368369 @bp.app_template_filter370 def my_reverse_2(s):371 return s[::-1]372373 @bp.app_template_filter("my_reverse_custom_name_3")374 def my_reverse_3(s):375 return s[::-1]376377 @bp.app_template_filter(name="my_reverse_custom_name_4")378 def my_reverse_4(s):379 return s[::-1]380381 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"385386 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"389390 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"393394 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"397398399def test_add_template_filter(app):400 bp = flask.Blueprint("bp", __name__)401402 def my_reverse(s):
122 unmodified lines
525 @bp.app_template_test()526 def is_boolean(value):527 return isinstance(value, bool)528529 @bp.app_template_test530 def boolean_2(value):531 return isinstance(value, bool)532533 @bp.app_template_test("my_boolean_custom_name")534 def boolean_3(value):535 return isinstance(value, bool)536537 @bp.app_template_test(name="my_boolean_custom_name_2")538 def boolean_4(value):539 return isinstance(value, bool)540541 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)545546 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)549550 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)553554 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)557558559def test_add_template_test(app):560 bp = flask.Blueprint("bp", __name__)561562 def is_boolean(value):
163 unmodified lines
726 @bp.app_template_global()727 def get_answer():728 return 42729730 @bp.app_template_global731 def get_stuff_1():732 return "get_stuff_1"733734 @bp.app_template_global("my_get_stuff_custom_name_2")735 def get_stuff_2():736 return "get_stuff_2"737738 @bp.app_template_global(name="my_get_stuff_custom_name_3")739 def get_stuff_3():740 return "get_stuff_3"741742 # 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 line
747 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"]() == 42750751 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"754755 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"758759 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"762763 with app.app_context():764 rv = flask.render_template_string("{{ get_answer() }}")765 assert rv == "42"766767 rv = flask.render_template_string("{{ get_stuff_1() }}")768 assert rv == "get_stuff_1"769770 rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")771 assert rv == "get_stuff_2"772773 rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")774 assert rv == "get_stuff_3"775776777def test_request_processing(app, client):778 bp = flask.Blueprint("bp", __name__)779 evts = []780
347 unmodified lines
modified:tests/test_templating.py+81 added
127 unmodified lines
128 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"131132 @app.template_filter133 def my_reverse_2(s):134 return s[::-1]135136 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"139140 @app.template_filter("my_reverse_custom_name_3")141 def my_reverse_3(s):142 return s[::-1]143144 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"147148 @app.template_filter(name="my_reverse_custom_name_4")149 def my_reverse_4(s):150 return s[::-1]151152 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"155156157def test_add_template_filter(app):158 def my_reverse(s):159 return s[::-1]160
85 unmodified lines
246 assert "boolean" in app.jinja_env.tests.keys()247 assert app.jinja_env.tests["boolean"] == boolean248 assert app.jinja_env.tests["boolean"](False)249250 @app.template_test251 def boolean_2(value):252 return isinstance(value, bool)253254 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)257258 @app.template_test("my_boolean_custom_name")259 def boolean_3(value):260 return isinstance(value, bool)261262 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)265266 @app.template_test(name="my_boolean_custom_name_2")267 def boolean_4(value):268 return isinstance(value, bool)269270 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)273274275def test_add_template_test(app):276 def boolean(value):277 return isinstance(value, bool)278
88 unmodified lines
367368 rv = flask.render_template_string("{{ get_stuff() }}")369 assert rv == "42"370371 @app.template_global372 def get_stuff_1():373 return "get_stuff_1"374375 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"378379 rv = flask.render_template_string("{{ get_stuff_1() }}")380 assert rv == "get_stuff_1"381382 @app.template_global("my_get_stuff_custom_name_2")383 def get_stuff_2():384 return "get_stuff_2"385386 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"389390 rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")391 assert rv == "get_stuff_2"392393 @app.template_global(name="my_get_stuff_custom_name_3")394 def get_stuff_3():395 return "get_stuff_3"396397 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"400401 rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")402 assert rv == "get_stuff_3"403404405def test_custom_template_loader(client):406 class MyFlask(flask.Flask):407 def create_global_jinja_loader(self):408 from jinja2 import DictLoader
124 unmodified lines
modified:CHANGES.rst+2 added
3 unmodified lines
4Unreleased56- 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`101112Version 3.1.113-------------
1611 unmodified lines