Added template_global, fixes #657
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 05f1623 · Browse files · 4 files·+74 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+74 added−0 removed
- other:+74 added−0 removed
Provenance
- Commit
3b393f8- Tree
562b1cf- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
05f1623
4 files changed+74 added−0 removed
modified:flask/testsuite/templating.py+12 added
254 unmodified lines255 return flask.render_template('template_test.html', value=False)256 rv = app.test_client().get('/')257 self.assert_('Success!' in rv.data)258 259+ def test_add_template_global(self):260+ app = flask.Flask(__name__)261+ @app.template_global()262+ def get_stuff():263+ return 42264+ self.assert_('get_stuff' in app.jinja_env.globals.keys())265+ self.assert_equal(app.jinja_env.globals['get_stuff'], get_stuff)266+ self.assert_(app.jinja_env.globals['get_stuff'](), 42)267+ with app.app_context():268+ rv = flask.render_template_string('{{ get_stuff() }}')269+ self.assert_equal(rv, '42')270+271 def test_custom_template_loader(self):272 class MyFlask(flask.Flask):273 def create_global_jinja_loader(self):274 from jinja2 import DictLoader30 unmodified linesmodified:flask/app.py+32 added
1164 unmodified lines1165 self.jinja_env.tests[name or f.__name__] = f1166 1167 1168 @setupmethod1169+ def template_global(self, name=None):1170+ """A decorator that is used to register a custom template global function.1171+ You can specify a name for the global function, otherwise the function1172+ name will be used. Example::1173+1174+ @app.template_global()1175+ def double(n):1176+ return 2 * n1177+1178+ .. versionadded:: 0.101179+1180+ :param name: the optional name of the global function, otherwise the1181+ function name will be used.1182+ """1183+ def decorator(f):1184+ self.add_template_global(f, name=name)1185+ return f1186+ return decorator1187+1188+ @setupmethod1189+ def add_template_global(self, f, name=None):1190+ """Register a custom template global function. Works exactly like the1191+ :meth:`template_global` decorator.1192+1193+ .. versionadded:: 0.101194+1195+ :param name: the optional name of the global function, otherwise the1196+ function name will be used.1197+ """1198+ self.jinja_env.globals[name or f.__name__] = f1199+1200+ @setupmethod1201 def before_request(self, f):1202 """Registers a function to run before each request."""1203 self.before_request_funcs.setdefault(None, []).append(f)1204 return f612 unmodified linesmodified:flask/blueprints.py+28 added
235 unmodified lines236 def register_template(state):237 state.app.jinja_env.tests[name or f.__name__] = f238 self.record_once(register_template)239 240+ def app_template_global(self, name=None):241+ """Register a custom template global, available application wide. Like242+ :meth:`Flask.template_global` but for a blueprint.243+244+ .. versionadded:: 0.10245+246+ :param name: the optional name of the global, otherwise the247+ function name will be used.248+ """249+ def decorator(f):250+ self.add_app_template_global(f, name=name)251+ return f252+ return decorator253+254+ def add_app_template_global(self, f, name=None):255+ """Register a custom template global, available application wide. Like256+ :meth:`Flask.add_template_global` but for a blueprint. Works exactly257+ like the :meth:`app_template_global` decorator.258+259+ .. versionadded:: 0.10260+261+ :param name: the optional name of the global, otherwise the262+ function name will be used.263+ """264+ def register_template(state):265+ state.app.jinja_env.globals[name or f.__name__] = f266+ self.record_once(register_template)267+268 def before_request(self, f):269 """Like :meth:`Flask.before_request` but for a blueprint. This function270 is only executed before each request that is handled by a function of271 that blueprint.130 unmodified linesmodified:CHANGES+2 added
11 unmodified lines12 limit the impact an attacker can do if the secret key leaks. See13 :ref:`upgrading-to-010` for more information.14 - Added ``template_test`` methods in addition to the already existing15 ``template_filter`` method family.16+- Added ``template_global`` methods in addition to the already existing17+ ``template_filter`` method family.18 - Set the content-length header for x-sendfile.19 - ``tojson`` filter now does not escape script blocks in HTML5 parsers.20 - Flask will now raise an error if you attempt to register a new function21 on an already used endpoint.397 unmodified lines