0
0
Flaskframework~5 mins

Template inheritance with extends in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the {% extends %} tag in Flask templates?
The {% extends %} tag allows a template to inherit from a base template, so you can reuse common layout and structure across multiple pages.
Click to reveal answer
beginner
How do you define a block in a Flask template?
You define a block using {% block block_name %} to start and {% endblock %} to end. This marks a section that child templates can override.
Click to reveal answer
intermediate
What happens if a child template does not override a block defined in the parent template?
If the child template does not override a block, the content inside that block from the parent template is used as default.
Click to reveal answer
beginner
Why is template inheritance useful in web development?
It helps keep code DRY (Don't Repeat Yourself) by sharing common layout and design in one place, making maintenance easier and consistent across pages.
Click to reveal answer
intermediate
Show a simple example of a base template with a block and a child template that extends it.
Base template (base.html):
<html>
  <body>
    <header>My Site</header>
    {% block content %}Default content{% endblock %}
  </body>
</html>

Child template (home.html):
{% extends 'base.html' %}
{% block content %}Welcome to the homepage!{% endblock %}
Click to reveal answer
What does the {% extends 'base.html' %} line do in a Flask template?
AIt includes the content of 'base.html' directly without inheritance.
BIt tells the template to use 'base.html' as a parent layout.
CIt imports Python code from 'base.html'.
DIt creates a new template file named 'base.html'.
Which tag is used to define a replaceable section in a Flask template?
A<code>{% block %}</code>
B<code>{% import %}</code>
C<code>{% extends %}</code>
D<code>{% include %}</code>
If a child template does not override a block, what content is shown?
AAn error is raised.
BThe block is empty.
CThe parent template's block content is shown.
DThe child template's content is shown instead.
Why should you use template inheritance in Flask?
ATo avoid repeating common layout code across pages.
BTo write all HTML in one file.
CTo speed up server response time.
DTo include JavaScript files automatically.
Which of these is a correct way to override a block named 'content' in a child template?
A{% include content %}New content{% endinclude %}
B{% extends content %}New content{% endextends %}
C{% import content %}New content{% endimport %}
D{% block content %}New content{% endblock %}
Explain how template inheritance works in Flask using the {% extends %} and {% block %} tags.
Think of a parent layout with placeholders that children fill in.
You got /4 concepts.
    Describe why using template inheritance is beneficial when building web pages with Flask.
    Imagine updating a header on many pages at once.
    You got /4 concepts.