Skip to content
Myr

subclassing.rst

This file

Path

docs/patterns/subclassing.rst

22 lines · 792 bytes

Last changed by

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

History of this file

Take it

1Subclassing Flask2=================34The :class:`~flask.Flask` class is designed for subclassing.56One reason to subclass would be customizing the Jinja2 :class:`~jinja2.Environment`. For example, to add a new global template variable::78    from flask import Flask9    from datetime import datetime1011    class MyFlask(Flask):12        """ Flask with more global template vars """1314        def create_jinja_environment(self):15            """ Initialize my custom Jinja environment. """16            jinja_env = super(MyFlask, self).create_jinja_environment(self)17            jinja_env.globals.update(18                current_time = datetime.datetime.now()19            )20            return jinja_env2122This is the recommended approach for overriding or augmenting Flask's internal functionality.