Discover how simple logic in templates saves you from endless manual HTML updates!
Why Control structures (if, for) in Flask? - Purpose & Use Cases
Imagine you have a webpage that needs to show a list of users and highlight those who are online. Without control structures, you'd have to write separate HTML for each user manually.
Manually writing HTML for each condition or list item is slow, repetitive, and prone to mistakes. If the user list changes, you must update the HTML every time, which is tiring and error-prone.
Flask templates let you use if and for control structures to automatically decide what to show and loop through lists. This means your HTML updates dynamically based on your data.
<ul> <li>Alice (online)</li> <li>Bob</li> <li>Charlie (online)</li> </ul>
<ul>
{% for user in users %}
{% if user.online %}
<li>{{ user.name }} (online)</li>
{% else %}
<li>{{ user.name }}</li>
{% endif %}
{% endfor %}
</ul>You can create flexible, dynamic webpages that automatically adjust content based on your data without rewriting HTML.
Showing a product list on an online store where items on sale are highlighted, and the list updates as products change.
Control structures let you add logic inside your HTML templates.
if helps show or hide content based on conditions.
for helps repeat HTML for each item in a list automatically.
Practice
{% if %} statement do in a Flask template?Solution
Step 1: Understand the purpose of
The{% if %}{% 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 DQuick Check:
{% if %}controls conditional display [OK]
- Confusing if with for loop
- Thinking if defines functions
- Assuming if imports templates
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 AQuick Check:
Flask uses {% for variable in iterable %} [OK]
- Using 'loop' or 'foreach' instead of 'for'
- Using 'over' instead of 'in'
- Missing curly braces or percent signs
{% for color in ['red', 'green', 'blue'] %}
{{ color }}
{% endfor %}What will be the rendered HTML output?
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 CQuick Check:
Loop prints each color in <p> tags [OK]
- Printing the variable name instead of its value
- Missing endfor tag causing syntax error
- Expecting plain text without tags
{% for user in users %}
{{ user.name }}
{% endfor %}Assuming
users is an empty list, what happens?Solution
Step 1: Check loop behavior with empty list
Whenusersis 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 BQuick Check:
Empty list means zero loop runs, zero output [OK]
- Expecting an error when list is empty
- Thinking endfor is missing
- Assuming output of empty tags
items is empty, otherwise list each item inside <li> tags within a <ul></ul>. Which Flask template code correctly does this?Solution
Step 1: Use if to check if list has items
{% if items %}-
{% for item in items %}
- {{ item }} {% endfor %}
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 %}
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 AQuick Check:
Use if-else with for loop for empty list check [OK]
- Using else inside for loop incorrectly
- Checking length with .length (not valid in Jinja)
- Using 'is empty' which is not valid syntax
Ready to prove it?
Pass the test and this pattern moves unknown
