1

I'm developing a flask extension following this tutorial. The part of my extension are also templates. I want, by default, to use templates from flask extension unless user overrides them in a main flask application. The problem is that by default template path points to main_flask_app/templates. How to go over it? Thanks a lot.

davidism
  • 121,510
  • 29
  • 395
  • 339
user3024710
  • 515
  • 1
  • 6
  • 15

1 Answers1

3

The layout of your app, and separately your extension, should look like this:

myapp_project/
    myapp/
        __init__.py
        models.py
        ...
        static/
        templates/
            index.html
            ...
            myext/
                mypage.html  # overrides default from ext

myext_project/
    myext/
        __init__.py
        ...
        templates/
            myext/
                mypage.html  # the default template from ext
            ...

Notice how the directory structure is the same. Adding a template with the same path to the app overrides the default on that path in the extension.

Your extension will make these templates available by registering a blueprint with the app. The blueprint should be set up to use the templates folder.

from flask import Blueprint

bp = Blueprint('myext', __name__, template_folder='templates')

class MyExt(object):
    ...
    def init_app(self, app):
        ...
        app.register_blueprint(bp)
    ...
davidism
  • 121,510
  • 29
  • 395
  • 339