📖 Scenario: You are building a website with Flask. You want to reuse a button design on many pages without repeating the HTML code.
🎯 Goal: Create a Jinja2 macro for a reusable button component. Use this macro in a template to show two buttons with different labels and links.
📋 What You'll Learn
Create a macro named button that takes label and url as parameters
Use the macro to render two buttons with different labels and URLs
Use semantic HTML and accessible attributes for the buttons
Keep the macro in a separate file and import it in the main template
💡 Why This Matters
🌍 Real World
Web developers often reuse UI components like buttons, forms, and cards across many pages. Macros help keep templates clean and consistent.
💼 Career
Knowing how to create and use macros in Flask templates is a valuable skill for backend web developers working with Python and Jinja2 templating.
Progress0 / 4 steps
1
Create the macro file with a button macro
Create a file named macros.html. Inside it, define a macro called button that takes label and url as parameters. The macro should output a <a> tag with href set to url, class btn, and the link text as label.
Flask
Hint
Use {% macro button(label, url) %} to start and {% endmacro %} to end the macro.
Inside, create an <a> tag with href and class.
2
Import the macro file in the main template
In your main template file named index.html, import the macro file macros.html using {% import 'macros.html' as macros %} at the top.
Flask
Hint
Use {% import 'macros.html' as macros %} at the top of index.html to access the macro.
3
Use the macro to create two buttons
In index.html, use the macro macros.button to create two buttons: one with label 'Home' and URL '/', and another with label 'Contact' and URL '/contact'. Place each button inside a <div>.
Flask
Hint
Call the macro with {{ macros.button('Home', '/') }} and {{ macros.button('Contact', '/contact') }} inside <div> tags.
4
Add CSS class for button styling
Add a CSS style block in index.html inside <style> tags. Define the .btn class with padding 0.5rem 1rem, background color #007BFF, text color white, border radius 0.25rem, and remove text underline.
Flask
Hint
Inside <style>, define the .btn class with the given styles for padding, background color, text color, border radius, and no underline.
Practice
(1/5)
1. What is the main purpose of using {% macro %} in Flask templates?
easy
A. To create reusable blocks of HTML code
B. To define Python functions inside templates
C. To import external CSS files
D. To handle form submissions
Solution
Step 1: Understand what macros do in Flask templates
Macros are used to write reusable HTML blocks that can be called multiple times to avoid repetition.
Step 2: Compare options with macro purpose
Only To create reusable blocks of HTML code correctly describes macros as reusable HTML blocks. Other options describe unrelated tasks.
Final Answer:
To create reusable blocks of HTML code -> Option A
Quick Check:
Macros = reusable HTML blocks [OK]
Hint: Macros create reusable HTML snippets in templates [OK]
Common Mistakes:
Thinking macros define Python functions
Confusing macros with CSS imports
Assuming macros handle form data
2. Which of the following is the correct syntax to define a macro named button that takes a parameter label?
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.