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>
✗ Incorrect
The {% if %} tag starts a conditional block in Flask templates.
How do you end a for loop in a Flask template?
A{% endfor %}
B{% endloop %}
C{% endforeach %}
D{% end %}
✗ Incorrect
You end a for loop with {% endfor %} in Flask templates.
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
✗ Incorrect
If the condition is false and no else block exists, the content inside the if block is skipped.
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'>
✗ Incorrect
The correct syntax is {% for item in 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
✗ Incorrect
You can nest if statements inside for loops to control what is shown for each item.
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.
Practice
(1/5)
1. What does the {% if %} statement do in a Flask template?
easy
A. It repeats HTML for each item in a list.
B. It imports another template file.
C. It defines a function in the template.
D. It shows HTML only when a condition is true.
Solution
Step 1: Understand the purpose of {% if %}
The {% if %} statement checks a condition and controls whether some HTML is shown.
Step 2: Compare with other options
{% for %} repeats HTML, functions and imports are not done with {% if %}.
Final Answer:
It shows HTML only when a condition is true. -> Option D
Quick Check:
{% if %} controls conditional display [OK]
Hint: Remember: if = condition, for = repeat [OK]
Common Mistakes:
Confusing if with for loop
Thinking if defines functions
Assuming if imports templates
2. Which of the following is the correct syntax to start a for loop in a Flask template?
easy
A. {% for item in list %}
B. {% loop item in list %}
C. {% foreach item in list %}
D. {% iterate item over list %}
Solution
Step 1: Recall Flask template for loop syntax
The correct syntax uses {% for variable in iterable %}.
Step 2: Check options for exact match
Only {% for item in list %} matches the correct syntax exactly.
Final Answer:
{% for item in list %} -> Option A
Quick Check:
Flask uses {% for variable in iterable %} [OK]
Hint: For loops use 'for variable in list' syntax [OK]
Common Mistakes:
Using 'loop' or 'foreach' instead of 'for'
Using 'over' instead of 'in'
Missing curly braces or percent signs
3. Given this Flask template snippet:
{% for color in ['red', 'green', 'blue'] %}
{{ color }}
{% endfor %}
What will be the rendered HTML output?
medium
A. red green blue
B.
color
color
color
C.
red
green
blue
D. SyntaxError
Solution
Step 1: Understand the for loop iteration
The loop runs over the list ['red', 'green', 'blue'], assigning each to 'color'.
Step 2: Render each item inside <p> tags
Each iteration outputs <p> followed by the color name and </p>.
Final Answer:
<p>red</p><p>green</p><p>blue</p> -> Option C
Quick Check:
Loop prints each color in <p> tags [OK]
Hint: For loops output each item wrapped in HTML [OK]
Common Mistakes:
Printing the variable name instead of its value
Missing endfor tag causing syntax error
Expecting plain text without tags
4. Identify the error in this Flask template code:
{% for user in users %}
{{ user.name }}
{% endfor %}
Assuming users is an empty list, what happens?
medium
A. SyntaxError because endfor is missing.
B. No output is shown because the loop does not run.
C. Runtime error because user.name is undefined.
D. Outputs one empty <p></p> tag.
Solution
Step 1: Check loop behavior with empty list
When users is empty, the for loop body does not execute at all.
Step 2: Confirm no output or errors occur
No iterations means no <p> tags output, and no errors happen.
Final Answer:
No output is shown because the loop does not run. -> Option B
Quick Check:
Empty list means zero loop runs, zero output [OK]
Hint: Empty lists skip for loops silently [OK]
Common Mistakes:
Expecting an error when list is empty
Thinking endfor is missing
Assuming output of empty tags
5. You want to display a message "No items found" if a list items is empty, otherwise list each item inside <li> tags within a <ul></ul>. Which Flask template code correctly does this?
hard
A. {% if items %}
{% for item in items %}
{{ item }}
{% endfor %}
{% else %}
No items found
{% endif %}
B. {% for item in items %}
{{ item }}
{% else %}
No items found
{% endfor %}
C. {% if items.length > 0 %}
{% for item in items %}
{{ item }}
{% endfor %}
{% endif %}
No items found
D. {% if items is empty %}
No items found
{% else %}
{% for item in items %}
{{ item }}
{% endfor %}
{% endif %}
Solution
Step 1: Use if to check if list has items
{% if items %}
{% for item in items %}
{{ item }}
{% endfor %}
{% else %}
No items found
{% endif %} uses {% if items %} which is true if list is not empty.
Step 2: Use else to show message when empty
{% if items %}
{% for item in items %}
{{ item }}
{% endfor %}
{% else %}
No items found
{% endif %} correctly uses {% else %} to show "No items found" if empty.
Final Answer:
Uses `{% if items %}` with `<ul>` and `{% else %}` for empty case -> Option A
Quick Check:
Use if-else with for loop for empty list check [OK]
Hint: Use if-else around for loop to handle empty lists [OK]
Common Mistakes:
Using else inside for loop incorrectly
Checking length with .length (not valid in Jinja)
Using 'is empty' which is not valid syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.