Skip to content
Myr

use template_filter without parens

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by kadai0308 · committed by David Lord · parent 85c5d93 · Browse files · 6 files+272 added−23 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+272 added−23 removed

  • tests:+162 added−0 removed
  • other:+110 added−23 removed

6 files+272 added−23 removed

modified:docs/templating.rst+5 added-1 removed
143 unmodified lines
144that. 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.147148The two following examples work the same and both reverse an object::148The following examples work the same and all reverse an object::149150 @app.template_filter # use the function name as filter name151 def reverse_filter(s):152 return s[::-1]153154 @app.template_filter('reverse')155 def reverse_filter(s):156 return s[::-1]157
76 unmodified lines
modified:src/flask/sansio/app.py+68 added-16 removed
660 unmodified lines
661 self.view_functions[endpoint] = view_func662663 @setupmethod664 def template_filter(665 self, name: str | None = None666 ) -> t.Callable[[T_template_filter], T_template_filter]:665 self, name: t.Callable[..., t.Any] | str | None = None666 ) -> 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 function669 name will be used. Example::670671 @app.template_filter()672 def reverse(s):673 return s[::-1]671 @app.template_filter()672 def reverse(s):673 return s[::-1]674675 The decorator also can be used without parentheses::676677 @app.template_filter678 def reverse(s):679 return s[::-1]680681 :param name: the optional name of the filter, otherwise the682 function name will be used.683 """684685 if callable(name):686 # If name is callable, it is the function to register.687 # This is a shortcut for the common case of688 # @app.template_filter689 # def func():690691 func = name692 self.add_template_filter(func)693 return func694695 def decorator(f: T_template_filter) -> T_template_filter:696 self.add_template_filter(f, name=name)697 return f698
12 unmodified lines
711 self.jinja_env.filters[name or f.__name__] = f712713 @setupmethod714 def template_test(699 self, name: str | None = None700 ) -> t.Callable[[T_template_test], T_template_test]:715 self, name: t.Callable[..., t.Any] | str | None = None716 ) -> 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 function719 name will be used. Example::720705 @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 False721 @app.template_test()722 def is_prime(n):723 if n == 2:724 return True725 for i in range(2, int(math.ceil(math.sqrt(n))) + 1):726 if n % i == 0:727 return False728 return True729730 The decorator also can be used without parentheses::731732 @app.template_test733 def is_prime(n):734 if n == 2:735 return True736 for i in range(2, int(math.ceil(math.sqrt(n))) + 1):737 if n % i == 0:738 return False739740 .. versionadded:: 0.10741742 :param name: the optional name of the test, otherwise the743 function name will be used.744 """745746 if callable(name):747 # If name is callable, it is the function to register.748 # This is a shortcut for the common case of749 # @app.template_test750 # def func():751752 func = name753 self.add_template_test(func)754 return func755756 def decorator(f: T_template_test) -> T_template_test:757 self.add_template_test(f, name=name)758 return f759
14 unmodified lines
774 self.jinja_env.tests[name or f.__name__] = f775776 @setupmethod777 def template_global(742 self, name: str | None = None743 ) -> t.Callable[[T_template_global], T_template_global]:778 self, name: t.Callable[..., t.Any] | str | None = None779 ) -> 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 function782 name will be used. Example::783784 @app.template_global()785 def double(n):786 return 2 * n787788 The decorator also can be used without parentheses::789790 @app.template_global791 def double(n):792 return 2 * n793794 .. versionadded:: 0.10795796 :param name: the optional name of the global function, otherwise the797 function name will be used.798 """799800 if callable(name):801 # If name is callable, it is the function to register.802 # This is a shortcut for the common case of803 # @app.template_global804 # def func():805806 func = name807 self.add_template_global(func)808 return func809810 def decorator(f: T_template_global) -> T_template_global:811 self.add_template_global(f, name=name)812 return f813
203 unmodified lines
modified:src/flask/sansio/blueprints.py+35 added-6 removed
440 unmodified lines
441 )442443 @setupmethod444 def app_template_filter(445 self, name: str | None = None446 ) -> t.Callable[[T_template_filter], T_template_filter]:445 self, name: t.Callable[..., t.Any] | str | None = None446 ) -> t.Callable[[T_template_filter], 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`.449450 :param name: the optional name of the filter, otherwise the451 function name will be used.452 """453454 if callable(name):455 # If name is callable, it is the function to register.456 # This is a shortcut for the common case of457 # @bp.add_template_filter458 # def func():459460 func = name461 self.add_app_template_filter(func)462 return func463464 def decorator(f: T_template_filter) -> T_template_filter:465 self.add_app_template_filter(f, name=name)466 return f467
17 unmodified lines
485 self.record_once(register_template)486487 @setupmethod488 def app_template_test(479 self, name: str | None = None480 ) -> t.Callable[[T_template_test], T_template_test]:489 self, name: t.Callable[..., t.Any] | str | None = None490 ) -> t.Callable[[T_template_test], T_template_test] | T_template_filter:491 """Register a template test, available in any template rendered by the492 application. Equivalent to :meth:`.Flask.template_test`.493494 .. versionadded:: 0.10
1 unmodified line
496 :param name: the optional name of the test, otherwise the497 function name will be used.498 """499500 if callable(name):501 # If name is callable, it is the function to register.502 # This is a shortcut for the common case of503 # @bp.add_template_test504 # def func():505506 func = name507 self.add_app_template_test(func)508 return func509510 def decorator(f: T_template_test) -> T_template_test:511 self.add_app_template_test(f, name=name)512 return f513
19 unmodified lines
533 self.record_once(register_template)534535 @setupmethod536 def app_template_global(517 self, name: str | None = None518 ) -> t.Callable[[T_template_global], T_template_global]:537 self, name: t.Callable[..., t.Any] | str | None = None538 ) -> t.Callable[[T_template_global], T_template_global] | T_template_filter:539 """Register a template global, available in any template rendered by the540 application. Equivalent to :meth:`.Flask.template_global`.541542 .. versionadded:: 0.10543544 :param name: the optional name of the global, otherwise the545 function name will be used.546 """547 if callable(name):548 # If name is callable, it is the function to register.549 # This is a shortcut for the common case of550 # @bp.add_template_global551 # def func():552553 func = name554 self.add_app_template_global(func)555 return func556557 def decorator(f: T_template_global) -> T_template_global:558 self.add_app_template_global(f, name=name)559 return f560
101 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