Creating a custom macro for forms
Macros allow us to write reusable pieces of HTML blocks. They are analogous to functions in regular programming languages. We can pass arguments to macros as we do with functions in Python, and we can then use them to process an HTML block. Macros can be called any number of times, and the output will vary as per the logic inside them. In this recipe, let’s understand how to write a macro in Jinja.
Getting ready
Macros in Jinja are a very common topic and have a lot of use cases. Here, we will just take a look at how a macro can be created and then used after importing it.
How to do it...
One of the most redundant pieces of code in HTML is that which defines input fields in forms. This is because most fields have similar code with maybe some style modifications.
The following snippet is a macro that creates input fields when invoked. The best practice is to create the macro in a separate file for better reusability – for...