0
0
Flaskframework

Macros for reusable components in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a macro in Flask's Jinja2 templates?
A macro is a reusable block of template code in Jinja2 that you can define once and use multiple times to avoid repetition.
Click to reveal answer
beginner
How do you define a macro in a Jinja2 template?
Use the {% macro macro_name(params) %} ... {% endmacro %} syntax to define a macro with optional parameters.
Click to reveal answer
beginner
How do you call a macro inside a Jinja2 template?
Call a macro by its name with parentheses and arguments if needed, like {{ macro_name(arg1, arg2) }}.
Click to reveal answer
intermediate
Why use macros for reusable components in Flask templates?
Macros help keep templates clean and DRY (Don't Repeat Yourself) by reusing common HTML or template code blocks easily.
Click to reveal answer
intermediate
How can you organize macros for reuse across multiple templates?
Put macros in a separate template file and import them using <code>{% import 'macros.html' as macros %}</code> to use in other templates.
Click to reveal answer
What is the correct way to define a macro named 'button' with a parameter 'label'?
A{{ macro button(label) }} ... {{ endmacro }}
B{% macro button(label) %} ... {% endmacro %}
C{% def button(label) %} ... {% enddef %}
D<macro name='button' param='label'> ... </macro>
How do you use a macro named 'button' with argument 'Click me' inside a template?
A{% button('Click me') %}
B{{ call button('Click me') }}
C{{ button('Click me') }}
D{% use button('Click me') %}
Which statement correctly imports macros from 'macros.html' as 'ui'?
A{% import 'macros.html' as ui %}
B{% include 'macros.html' as ui %}
C{% from 'macros.html' import ui %}
D{% use 'macros.html' as ui %}
Why are macros preferred over copying HTML code in multiple templates?
AThey reduce repetition and make maintenance easier.
BThey make templates load faster.
CThey automatically style components.
DThey prevent syntax errors.
If you have a macro 'button' imported as 'ui', how do you call it?
A{% button.ui() %}
B{{ button.ui() }}
C{% ui.button() %}
D{{ ui.button() }}
Explain how to create and use a macro for a reusable button component in Flask templates.
Think about how to avoid repeating the same button HTML everywhere.
You got /4 concepts.
    Describe the benefits of using macros for reusable components in Flask's Jinja2 templates.
    Consider how macros help when you update a component used many times.
    You got /4 concepts.