0
0
Flaskframework~20 mins

Include for reusable fragments in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Include Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask template using include?
Given the following Flask templates, what will be rendered when main.html is rendered?

base.html:
{% raw %}{% block content %}{% endblock %}{% endraw %}

header.html:
{% raw %}

Welcome!

{% endraw %}

main.html:
{% raw %}{% extends 'base.html' %}{% block content %}{% include 'header.html' %}

Main content here.

{% endblock %}{% endraw %}
A<header><h1>Welcome!</h1></header><p>Main content here.</p>
B<p>Main content here.</p>
C<header>Main content here.</header>
DError: 'include' tag not found
Attempts:
2 left
💡 Hint
Remember that {% raw %}{% include %}{% endraw %} inserts the content of the specified template at that point.
📝 Syntax
intermediate
2:00remaining
Which option correctly includes a reusable fragment in a Flask template?
You want to include a reusable footer fragment stored in footer.html inside your main template. Which of the following is the correct syntax?
A{% raw %}{% import 'footer.html' %}{% endraw %}
B{% raw %}{% require 'footer.html' %}{% endraw %}
C{% raw %}{% extends 'footer.html' %}{% endraw %}
D{% raw %}{% include 'footer.html' %}{% endraw %}
Attempts:
2 left
💡 Hint
The tag to insert another template's content is {% raw %}{% include %}{% endraw %}.
🔧 Debug
advanced
2:00remaining
Why does this Flask template raise a TemplateNotFound error?
You have this template code:
{% raw %}{% include 'nav.html' %}{% endraw %}

But when rendering, Flask raises TemplateNotFound: nav.html. What is the most likely cause?
AYou must use {% raw %}{% extends %}{% endraw %} instead of include.
BThe include tag is misspelled.
CThe file nav.html is missing from the templates folder.
DFlask does not support including templates.
Attempts:
2 left
💡 Hint
Check if the file you want to include exists in the right place.
state_output
advanced
2:00remaining
What is the output when including a template with variables?
Given these templates:

greeting.html:
{% raw %}

Hello, {{ name }}!

{% endraw %}

main.html:
{% raw %}{% include 'greeting.html' %}{% endraw %}


And the Flask view renders main.html with {'name': 'Alice'} as context, what will be the output?
A<p>Hello, Alice!</p>
B<p>Hello, {{ name }}!</p>
C<p>Hello, !</p>
DError: Variable 'name' not found
Attempts:
2 left
💡 Hint
Included templates share the same context as the parent template.
🧠 Conceptual
expert
3:00remaining
Why use {% raw %}{% include %}{% endraw %} instead of {% raw %}{% extends %}{% endraw %} for reusable fragments?
In Flask templates, what is the main conceptual difference between {% raw %}{% include %}{% endraw %} and {% raw %}{% extends %}{% endraw %} when building reusable parts?
A{% raw %}{% extends %}{% endraw %} inserts a fragment, while {% raw %}{% include %}{% endraw %} replaces the whole template.
B{% raw %}{% include %}{% endraw %} inserts a fragment inside a template, while {% raw %}{% extends %}{% endraw %} defines a base template to inherit from.
CBoth do the same thing but {% raw %}{% include %}{% endraw %} is faster.
D{% raw %}{% include %}{% endraw %} is deprecated and should not be used.
Attempts:
2 left
💡 Hint
Think about whether you want to build a whole page layout or just add a small piece.