Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the differences between import and extends in Flask?

I am reading 《Flask web development》. in Example 4-3,

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

I'd like to know: What are the differences between extends and import?(I think they are quite similar in usage.) In which situation,I will use extends or import?

like image 910
everfight Avatar asked Aug 16 '16 15:08

everfight


2 Answers

When you extend another template the template controls you (the called controls the caller) - only named blocks in the "parent" template will be rendered:

{% extends "base.html" %}
{% block main_content %}
Only shows up if there is a block called main_content
in base.html.
{% endblock main_content%}

On the other hand an import simply binds the template to a name in your template's scope, and you control when and where to call it (the caller controls the called):

{% import "bootstrap/wtf.html" as wtf %}
Some of your own template code with {{ wtf.calls() }} where it makes sense.
like image 119
Sean Vieira Avatar answered Nov 05 '22 11:11

Sean Vieira


There is a difference. {% extends parent.html %} allows you to render parent.html and also override {% block %}'s defined in it while {% import %} just allows you to access template variables.

So example template is extends base.html and imports variables from bootstrap/wtf.html. Think about it like python's class inheritance and import statement.

like image 45
vsminkov Avatar answered Nov 05 '22 12:11

vsminkov