3
votes

In Flask, there seem to be two ways of defining templates:

  1. App-level:

    app/
        templates/
            index.html
            user/
                index.html
    
  2. And Blueprint-level:

    app/
        user/
            templates/
                index.html
        templates/
            index.html
    

Both seem to work, but is one better than the other? If not, what are the benefits/drawbacks of each approach?

There isn't a whole lot of documentation on Blueprints, so any help would be greatly appreciated.

1

1 Answers

3
votes

I think drawbacks really depends on your plans, what you will do with app. If you will have templates in the blueprint level, then your app is more flexible in case you want to move blueprint from one app to another and still have everything in one place. Don't think that there should be something else except of your needs and architecture of app.

Also you can completely overwrite jinja2 template loader for your app and have templates in the directories you want :) So you can move all templates to the dirs you really want if you don't like the way it is now. Something like this:

import jinja2

my_loader = jinja2.ChoiceLoader([
    app.jinja_loader,
    jinja2.FileSystemLoader(['templates/somwehere',
                             'templates/another_place',
                             'apps/templates']),
])
app.jinja_loader = my_loader

Keep in mind that the order is important here.