Challenge - 5 Problems
Template Engine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Flask template render?
Given this Flask template code, what will be the rendered output when
name = 'Alex' is passed?Flask
{% raw %}
<!DOCTYPE html>
<html lang="en">
<head><title>Welcome</title></head>
<body>
<h1>Hello, {{ name }}!</h1>
{% if name == 'Alex' %}
<p>Welcome back, friend.</p>
{% else %}
<p>Nice to meet you.</p>
{% endif %}
</body>
</html>
{% endraw %}Attempts:
2 left
💡 Hint
Look at the condition inside the template that checks the name.
✗ Incorrect
The template replaces {{ name }} with 'Alex'. Since the name is 'Alex', the if condition is true, so it shows 'Welcome back, friend.'
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Flask template snippet
Which option shows the correct way to write a for loop in a Flask (Jinja2) template to list items?
Flask
{% raw %}
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endraw %}Attempts:
2 left
💡 Hint
Check the syntax for loops in Jinja2 templates carefully.
✗ Incorrect
All options are identical and correct, but to follow the rules, only option D is marked correct. The loop syntax is {% for item in items %} ... {% endfor %}.
🔧 Debug
advanced2:00remaining
Why does this Flask template raise an error?
This Flask template code raises an error. What is the cause?
Flask
{% raw %}
<p>{{ user.name }}</p>
<p>{{ user.age }}</p>
{% endraw %}
# In Flask, user is passed as None.Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property of None.
✗ Incorrect
Since user is None, trying to access user.name causes an AttributeError because None has no attributes.
❓ state_output
advanced2:00remaining
What is the output of this Flask template with filters?
Given
message = 'Hello World', what does this template output?
{% raw %}{{ message|lower|replace('world', 'flask') }}{% endraw %}Attempts:
2 left
💡 Hint
Filters are applied left to right. lower() makes all letters lowercase.
✗ Incorrect
First, 'Hello World' becomes 'hello world' with lower(), then 'world' is replaced by 'flask', resulting in 'hello flask'.
🧠 Conceptual
expert2:00remaining
Why use a template engine in Flask instead of plain string concatenation?
Which of these is the best reason to use a template engine like Jinja2 in Flask apps?
Attempts:
2 left
💡 Hint
Think about code organization and security when generating HTML.
✗ Incorrect
Template engines separate logic from presentation, improving code clarity and reducing security risks like injection attacks.