Macros for Repeated Markup
Write template functions to avoid copy-paste.
Macros for Repeated Markup is a free Flask Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Repeated Markup Begs for a Function
When the same input field or card markup appears with small tweaks, copying it gets messy. A macro turns that pattern into a reusable function. 🛠️
What a Macro Is
A macro is a template function: you define markup once with parameters, then call it many times with different arguments.
Define a Macro
You declare a macro with a name and parameters, write its markup, and close it with endmacro. It is reusable from then on.
{% macro field(name) %}
<input name="{{ name }}">
{% endmacro %}Call a Macro
Invoke a macro like a function, passing arguments. Jinja2 renders the macro body with those values right where you call it.
{{ field("email") }}Parameters Customize Output
Macro parameters let one definition produce many variations, such as different input types or labels, from a single source of markup.
{% macro field(name, type="text") %}
<input name="{{ name }}" type="{{ type }}">
{% endmacro %}Default Argument Values
Give parameters defaults so common calls stay short. Callers override a default only when they need something different.
{{ field("age", type="number") }}Keep Macros in Their Own File
Store shared macros in a file like forms.html so many templates can import and reuse the same helpers.
{% import "forms.html" as forms %}Import Then Call
After import, call a macro through its namespace. This keeps your macro library separate from the page using it.
{{ forms.field("email") }}Import Specific Macros
Use from import to pull just the macros you need by name, keeping the template clean and the intent obvious.
{% from "forms.html" import field %}Macros vs Includes
An include drops fixed markup; a macro takes arguments and varies its output. Reach for macros when the snippet must change per call.
DRY Templates at Last
Macros remove copy-paste from your templates: fix the field markup once and every call site updates. That is templating done right. ✨
Quick Check
What makes a macro different from an include?
Recap: Macros
A macro is a reusable, parameterized template function. Define it once, import it, and call it anywhere to keep templates DRY. 🎯
Frequently asked questions
Is the “Macros for Repeated Markup” lesson free?
Yes — the full text of “Macros for Repeated Markup” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Macros for Repeated Markup”?
Write template functions to avoid copy-paste. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Macros for Repeated Markup” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Define a Base Template with Blocks
- Extend the Base in Child Pages
- Include Reusable Partials
- Macros for Repeated Markup