Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a base Flask Jinja template from a Blueprint template?

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?

like image 627
Kyle Johnson Avatar asked Jun 27 '12 19:06

Kyle Johnson


People also ask

How do I extend a base template in Flask?

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.

How do I import blueprints into Flask?

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.

How do I use Jinja extends?

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.

What is Jinja file extension?

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 , .


1 Answers

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.

like image 152
codecool Avatar answered Nov 09 '22 04:11

codecool