forked from orbit-oss/flask
Reword context processors for functions.
This commit is contained in:
parent
d33f9990c8
commit
8ef2ca99b9
1 changed files with 14 additions and 12 deletions
|
|
@ -147,6 +147,8 @@ autoescape %}`` block:
|
||||||
Whenever you do this, please be very cautious about the variables you are
|
Whenever you do this, please be very cautious about the variables you are
|
||||||
using in this block.
|
using in this block.
|
||||||
|
|
||||||
|
.. _registering-filters:
|
||||||
|
|
||||||
Registering Filters
|
Registering Filters
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|
@ -176,7 +178,7 @@ context processors exist in Flask. Context processors run before the
|
||||||
template is rendered and have the ability to inject new values into the
|
template is rendered and have the ability to inject new values into the
|
||||||
template context. A context processor is a function that returns a
|
template context. A context processor is a function that returns a
|
||||||
dictionary. The keys and values of this dictionary are then merged with
|
dictionary. The keys and values of this dictionary are then merged with
|
||||||
the template context::
|
the template context, for all templates in the app::
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_user():
|
def inject_user():
|
||||||
|
|
@ -187,21 +189,21 @@ the template with the value of `g.user`. This example is not very
|
||||||
interesting because `g` is available in templates anyways, but it gives an
|
interesting because `g` is available in templates anyways, but it gives an
|
||||||
idea how this works.
|
idea how this works.
|
||||||
|
|
||||||
It is also possible to inject functions that can have any number of
|
Variables are not limited to values; a context processor can also make
|
||||||
arguments::
|
functions available to templates (since Python allows passing around
|
||||||
|
functions)::
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def price_formatter():
|
def utility_processor():
|
||||||
def loader(amount, currency=u'€'):
|
def format_price(amount, currency=u'€'):
|
||||||
return u'{0:.2f}{1}.format(amount, currency)
|
return u'{0:.2f}{1}.format(amount, currency)
|
||||||
return dict(format_price=loader)
|
return dict(format_price=format_price)
|
||||||
|
|
||||||
The above construct registers a "variable" function called
|
The context processor above makes the `format_price` function available to all
|
||||||
`format_price` which can then be used in template::
|
templates::
|
||||||
|
|
||||||
{{ format_price(0.33) }}
|
{{ format_price(0.33) }}
|
||||||
|
|
||||||
The difference from regular context processor' variables is that functions
|
You could also build `format_price` as a template filter (see
|
||||||
are evaluated upon template rendering compared to variables whose values
|
:ref:`registering-filters`), but this demonstrates how to pass functions in a
|
||||||
are created during `app` startup . Therefore "variable" functions make it
|
context processor.
|
||||||
possible to inject dynamic data into templates.
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue