Hint: Use {% macro %} and {% endmacro %} to define macros [OK]
Common Mistakes:
Using {{ }} instead of {% %} for macro definition
Using incorrect keywords like define or <% %>
Forgetting the endmacro tag
3. Given the macro defined in macros.html as:
{% raw %}{% macro greet(name) %}Hello, {{ name }}!{% endmacro %}{% endraw %}
And imported in index.html with:
{% raw %}{% import 'macros.html' as m %}{% endraw %}
What will be the output of {{ m.greet('Alice') }} in index.html?
medium
A. Error: macro not found
B. {{ m.greet('Alice') }}
C. greet('Alice')
D. Hello, Alice!
Solution
Step 1: Understand macro import and usage
The macro greet(name) returns 'Hello, {{ name }}!'. Importing as m allows calling m.greet('Alice').
Step 2: Evaluate the macro call output
Calling m.greet('Alice') replaces {{ name }} with 'Alice', so output is 'Hello, Alice!'.
Final Answer:
Hello, Alice! -> Option D
Quick Check:
Macro call with parameter = rendered string [OK]
Hint: Imported macros called with alias render output [OK]
Common Mistakes:
Expecting literal macro call text instead of rendered output
Forgetting to import macros before use
Confusing macro call syntax with variable output
4. What is wrong with this macro usage in a Flask template?
{% raw %}{% macro card(title) %}
{{ title }}
{% endmacro %}{% endraw %} {{ card() }}
medium
A. The macro is not properly closed with {% endmacro %}
B. Macros cannot return HTML elements
C. The macro is called without the required parameter
D. The macro name is invalid
Solution
Step 1: Check macro definition and parameters
The macro card requires one parameter: title.
Step 2: Check macro call
The call {{ card() }} provides no argument, causing an error due to missing parameter.
Final Answer:
The macro is called without the required parameter -> Option C
Quick Check:
Missing macro argument = error [OK]
Hint: Always pass required parameters when calling macros [OK]
Common Mistakes:
Calling macros without required arguments
Thinking macros must return plain text only
Missing endmacro tag (not the case here)
5. You want to create a reusable navigation bar macro that highlights the current page. Which approach correctly uses macros and parameters to achieve this?
hard
A. {% raw %}{% macro navbar(current) %}{% endmacro %}{% endraw %} Then call with {{ navbar('about') }}
B. {% raw %}{% macro navbar() %}{% endmacro %}{% endraw %} Then call with {{ navbar() }}
C. {% raw %}{% macro navbar(current) %}{% endmacro %}{% endraw %} Then call with {{ navbar('home') }}
D. {% raw %}{% macro navbar(current) %}{% endmacro %}{% endraw %} Then call with {{ navbar('home') }}
Solution
Step 1: Understand the need for dynamic class assignment
The macro should accept a parameter to know which page is current and add 'active' class accordingly.
Step 2: Analyze each option's approach
{% raw %}{% macro navbar(current) %}{% endmacro %}{% endraw %} Then call with {{ navbar('about') }} uses a parameter and conditional expression to add 'active' class only to the current page link. Others either hardcode classes or ignore the parameter.
Final Answer:
Uses parameters and conditional logic to highlight current page -> Option A
Quick Check:
Dynamic class with parameter = correct navbar macro [OK]
Hint: Use macro parameters with conditionals for dynamic HTML [OK]
Common Mistakes:
Hardcoding active class without parameters
Ignoring the current page parameter
Assigning 'current' class to all links
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.