forked from orbit-oss/flask
Add subclassing pattern/example to fix issue #221.
This commit is contained in:
parent
41f3d67dff
commit
63b5dab0fc
3 changed files with 24 additions and 1 deletions
|
|
@ -41,3 +41,4 @@ Snippet Archives <http://flask.pocoo.org/snippets/>`_.
|
|||
methodoverrides
|
||||
requestchecksum
|
||||
celery
|
||||
subclassing
|
||||
|
|
|
|||
22
docs/patterns/subclassing.rst
Normal file
22
docs/patterns/subclassing.rst
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Subclassing Flask
|
||||
=================
|
||||
|
||||
The :class:`~flask.Flask` class is designed for subclassing.
|
||||
|
||||
One reason to subclass would be customizing the Jinja2 :class:`~jinja2.Environment`. For example, to add a new global template variable::
|
||||
|
||||
from flask import Flask
|
||||
from datetime import datetime
|
||||
|
||||
class MyFlask(Flask):
|
||||
""" Flask with more global template vars """
|
||||
|
||||
def create_jinja_environment(self):
|
||||
""" Initialize my custom Jinja environment. """
|
||||
jinja_env = super(MyFlask, self).create_jinja_environment(self)
|
||||
jinja_env.globals.update(
|
||||
current_time = datetime.datetime.now()
|
||||
)
|
||||
return jinja_env
|
||||
|
||||
This is the recommended approach for overriding or augmenting Flask's internal functionality.
|
||||
Loading…
Add table
Add a link
Reference in a new issue