0
0
Flaskframework~20 mins

Block definitions and overriding in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Block Mastery in Flask Templates
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Flask template inheritance?

Given the base template defines a block and the child template overrides it, what will the final rendered HTML output be?

Flask
{% raw %}
<!-- base.html -->
<html>
  <body>
    {% block content %}Base Content{% endblock %}
  </body>
</html>

<!-- child.html -->
{% extends 'base.html' %}
{% block content %}Child Content{% endblock %}
{% endraw %}
A<html>\n <body>\n Child Content\n </body>\n</html>
B<html>\n <body>\n Base Content\n </body>\n</html>
C<html>\n <body>\n {% block content %}Child Content{% endblock %}\n </body>\n</html>
D<html>\n <body>\n {% block content %}Base Content{% endblock %}\n </body>\n</html>
Attempts:
2 left
💡 Hint

Remember that child templates replace blocks defined in the base template.

📝 Syntax
intermediate
1:30remaining
Which option correctly overrides a block in Flask Jinja2?

Identify the correct syntax to override a block named header in a child template.

A{% block header %}New Header%
B{% override header %}New Header{% endoverride %}
C{% block header %}New Header{% endblock %}
D{% block header %}New Header{% endblock
Attempts:
2 left
💡 Hint

Blocks must start with {% block name %} and end with {% endblock %}.

🔧 Debug
advanced
2:00remaining
Why does this Flask template raise a TemplateSyntaxError?

Examine the child template code below. Why does it cause a TemplateSyntaxError when rendering?

Flask
{% raw %}
{% extends 'base.html' %}
{% block content %}
  <p>Welcome!</p>
{% endblock content %}
{% endraw %}
ABecause the block content must be empty
BBecause the block end tag should be {% endblock %} without the block name
CBecause the block name 'content' is invalid
DBecause the extends tag must be at the end of the file
Attempts:
2 left
💡 Hint

Check the syntax rules for ending blocks in Jinja2.

state_output
advanced
2:30remaining
What is the output of this nested block override in Flask templates?

Given the following base and child templates, what will be the rendered output?

Flask
{% raw %}
<!-- base.html -->
<html>
  <body>
    {% block main %}
      Main Base
      {% block sidebar %}Sidebar Base{% endblock %}
    {% endblock %}
  </body>
</html>

<!-- child.html -->
{% extends 'base.html' %}
{% block main %}
  Main Child
  {% block sidebar %}Sidebar Child{% endblock %}
{% endblock %}
{% endraw %}
A<html>\n <body>\n Main Base\n Sidebar Base\n </body>\n</html>
B<html>\n <body>\n Main Child\n Sidebar Base\n </body>\n</html>
C<html>\n <body>\n Main Base\n Sidebar Child\n </body>\n</html>
D<html>\n <body>\n Main Child\n Sidebar Child\n </body>\n</html>
Attempts:
2 left
💡 Hint

Remember that nested blocks can be overridden independently in child templates.

🧠 Conceptual
expert
3:00remaining
Which statement about Flask Jinja2 block overriding is TRUE?

Choose the correct statement about how block overriding works in Flask Jinja2 templates.

ABlocks in child templates completely replace the base block content unless <code>super()</code> is called inside the block.
BA child template can override a block defined in the base template, but cannot extend the base block content.
CBlocks can only be overridden if they are defined at the top level of the base template, not nested inside other blocks.
DIf a block is not overridden in the child template, it will cause a rendering error.
Attempts:
2 left
💡 Hint

Think about how to include base block content when overriding.