What are the differences between importing and expanding in Flask?

advertisements

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.