I am creating a fairly large application using Flask and Jinja. Flask recommends separating large applications into smaller units using Blueprints. If I have a base layout for my entire application/website, how can I extend this from templates within my blueprints?
Here, you use the {% extends %} tag to inherit from the base. html template. You then extend it via replacing the content block in the base template with what is inside the content block in the preceding code block.
To use any Flask Blueprint, you have to import it and then register it in the application using register_blueprint() . When a Flask Blueprint is registered, the application is extended with its contents. While the application is running, go to http://localhost:5000 using your web browser.
To reuse a Jinja template you use the Jinja built-in {% extends %} tag. The {% extends %} tag uses the syntax {% extends <name> %} to reuse the layout of another template. This means that in order to reuse the layout in listing 4-5 defined in a file base. html , you use the syntax {% extends "base.
Synopsis. A Jinja template is simply a text file. Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn't need to have a specific extension: . html , .
You simply write name of the base template layout and Flask will find it if it exists in app's templates folder and then in blueprint's templates folder.
{% extends 'template_name.html' %}
If it exists inside a folder in templates folder then
{% extends 'folder_name/template_name.html' %}
If there are two templates with same name in app's templates folder and blueprint's template folder, then template in app's templates folder will get priority.
See this question for more info flask blueprint template folder
Flask automatically finds templates for you if they are placed at right positions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With