0
0
Flaskframework~5 mins

Control structures (if, for) in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the {% if %} statement in Flask templates?
The {% if %} statement is used to conditionally display parts of the HTML based on a condition. It helps show or hide content dynamically.
Click to reveal answer
beginner
How do you loop over a list in a Flask template?
You use the {% for item in list %} statement to repeat HTML for each item in the list. It helps display multiple items like a list or table rows.
Click to reveal answer
beginner
What happens if the {% if %} condition is false in a Flask template?
The HTML inside the {% if %} block is skipped and not shown. You can use {% else %} to show alternative content.
Click to reveal answer
intermediate
Can you nest {% for %} loops inside each other in Flask templates?
Yes, you can nest {% for %} loops to handle complex data like lists of lists. Just make sure to use different variable names to avoid confusion.
Click to reveal answer
beginner
What is the syntax to end a control structure like {% if %} or {% for %} in Flask templates?
You close control structures with {% endif %} for if and {% endfor %} for for loops.
Click to reveal answer
Which tag starts a conditional block in a Flask template?
A{% if %}
B{% for %}
C{{ if }}
D<if>
How do you end a for loop in a Flask template?
A{% endfor %}
B{% endloop %}
C{% endforeach %}
D{% end %}
What will happen if the condition in {% if %} is false and there is no {% else %}?
AThe whole template stops rendering
BAn error occurs
CThe if block content is shown anyway
DNothing inside the if block is shown
Which of these is a correct way to loop over a list named 'items' in Flask template?
A{% loop item in items %}
B{% for item in items %}
C{{ for item in items }}
D<for each='items'>
Can you use an if statement inside a for loop in Flask templates?
ANo, only one control structure per template
BOnly if you use special syntax
CYes, nesting control structures is allowed
DOnly outside the loop
Explain how to use {% if %} and {% for %} in a Flask template to show a list only if it has items.
Think about showing content only when there is something to show.
You got /3 concepts.
    Describe how nesting a for loop inside an if statement works in Flask templates.
    Imagine checking a condition before listing items.
    You got /4 concepts.