Challenge - 5 Problems
Macro Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does this Flask macro render?
Given the macro and its usage below, what HTML output will be rendered?
Flask
{% macro button(text, type='button') %}
<button type="{{ type }}">{{ text }}</button>
{% endmacro %}
{{ button('Click me', 'submit') }}Attempts:
2 left
💡 Hint
Look at how the macro uses the parameters and what is passed when calling it.
✗ Incorrect
The macro defines a button with a type attribute defaulting to 'button'. When called with 'submit', it replaces the type attribute. The text inside the button is 'Click me'.
📝 Syntax
intermediate1:00remaining
Identify the syntax error in this macro definition
Which option shows the syntax error in the macro below?
Flask
{% macro list_items(items) %}
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endmacro %}Attempts:
2 left
💡 Hint
Check Jinja2 macro and loop syntax carefully.
✗ Incorrect
The macro is correctly defined with proper Jinja2 syntax. The for loop and macro tags are correct.
❓ state_output
advanced2:00remaining
What is the output of this macro call with nested macros?
Consider these macros and the call below. What HTML is produced?
Flask
{% macro badge(text) %}
<span class="badge">{{ text }}</span>
{% endmacro %}
{% macro user_info(name, status) %}
<div class="user">
<h2>{{ name }}</h2>
{{ badge(status) }}
</div>
{% endmacro %}
{{ user_info('Alice', 'Active') }}Attempts:
2 left
💡 Hint
Look at how the user_info macro calls the badge macro inside it.
✗ Incorrect
The user_info macro outputs a div with the name in h2 and calls badge macro to render the status inside a span with class 'badge'.
🔧 Debug
advanced1:30remaining
Why does this macro raise an error?
This macro is called with None but raises an error. What is the cause?
Flask
{% macro first_item(items) %}
{{ items[0] }}
{% endmacro %}
{{ first_item(None) }}Attempts:
2 left
💡 Hint
Think about what happens when you try to access index 0 of None.
✗ Incorrect
Calling items[0] when items is None causes a TypeError because NoneType does not support indexing.
🧠 Conceptual
expert1:30remaining
Which statement about Flask macros is true?
Choose the correct statement about macros in Flask's Jinja2 templates.
Attempts:
2 left
💡 Hint
Think about what macros do in templates.
✗ Incorrect
Macros are reusable template snippets that accept parameters and generate HTML. They do not affect server-side variables directly, are defined in templates, and run synchronously.