What is Jinja?
Jinja is a text rendering engine for Python programming language. Armin Ronacher created it and is available under a BSD software licence. It has first-class support among popular Python frameworks like Django and Flask and is used extensively. It is popular for its easy and straightforward syntax and variety of features including (but not limited to) –
- Variable access.
- Control structures (Conditionals, loops).
- Macros (similar to functions in programming languages).
- Filters
- Inheritance, etc.
Its syntax is heavily influenced by Django and Python which is good for anyone already familiar with Python.
How to use Macros in Jinja
Macros are a powerful functionality provided by Jinja, a text rendering engine for Python programming language. Macros in Jinja are similar to functions in other programming languages and help to implement the DRY Principle of programming (i.e., Don’t Repeat Yourself) in the Jinja templates by helping to avoid repeated code. It is fairly easy to use macros. This article intends to show how to use macros in Jinja. It assumes basic familiarity with Python programming language and Jinja.