Skip to content
Myr

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

4 files+74 added−0 removed

modified:flask/testsuite/templating.py+12 added
254 unmodified lines
255 return flask.render_template('template_test.html', value=False)256 rv = app.test_client().get('/')257 self.assert_('Success!' in rv.data)258259 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')270271 def test_custom_template_loader(self):272 class MyFlask(flask.Flask):273 def create_global_jinja_loader(self):274 from jinja2 import DictLoader
30 unmodified lines
modified:flask/app.py+32 added
1164 unmodified lines
1165 self.jinja_env.tests[name or f.__name__] = f116611671168 @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::11731174 @app.template_global()1175 def double(n):1176 return 2 * n11771178 .. versionadded:: 0.1011791180 :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 decorator11871188 @setupmethod1189 def add_template_global(self, f, name=None):1190 """Register a custom template global function. Works exactly like the1191 :meth:`template_global` decorator.11921193 .. versionadded:: 0.1011941195 :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__] = f11991200 @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 f
612 unmodified lines
modified:flask/blueprints.py+28 added
235 unmodified lines
236 def register_template(state):237 state.app.jinja_env.tests[name or f.__name__] = f238 self.record_once(register_template)239240 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.243244 .. versionadded:: 0.10245246 :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 decorator253254 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.258259 .. versionadded:: 0.10260261 :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)267268 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 lines
modified:CHANGES+2 added
11 unmodified lines
12 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