Skip to content
Myr

Document context processors' variable functions

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

authored by Priit Laes · committed by Priit Laes · parent 7ba5196 · Browse files · 1 file+19 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

1 file+19 added−0 removed

  • other:+19 added−0 removed

1 file+19 added−0 removed

modified:docs/templating.rst+19 added
184 unmodified lines
185The context processor above makes a variable called `user` available in186the template with the value of `g.user`. This example is not very187interesting because `g` is available in templates anyways, but it gives an188idea how this works.189190It is also possible to inject functions that can have any number of191arguments::192193 @app.context_processor194 def price_formatter():195 def loader(amount, currency=u'€'):196 return u'{0:.2f}{1}.format(amount, currency)197 return dict(format_price=loader)198199The above construct registers a "variable" function called200`format_price` which can then be used in template::201202 {{ format_price(0.33) }}203204The difference from regular context processor' variables is that functions205are evaluated upon template rendering compared to variables whose values206are created during `app` startup . Therefore "variable" functions make it207possible to inject dynamic data into templates.