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