Advanced examples of using Jinja Macros

In bigger projects, it is often convenient to keep functions in separate files and then import and call them in files wherever needed. We can do the same with macros too. We can define the macros in separate jinja file(s) and import one or more of these files as per requirement in any file to use the macros.

Example: Keeping Jinja macros in separate file(s)

Create a file named “myMacroCollection.jinja” inside “templates” folder. This will contain our macros. Create a file named “importExampleTemplate.jinja” inside “templates” folder. This file will import and call macros from “myMacroCollection” file. Create a file named “importExampleTemplate.py” inside “jinjaTests” folder. This will render and show the output of “importExampleTemplate.jinja” template on screen.

Add this first macro to “myMacroCollection.jinja” file –

{%macro hello() %}
{# This macro just renders string Hello #}
Hello!
{%endmacro%}

Now here are the contents of the “importExampleTemplate.jinja” file –

{% import 'myMacroCollection.jinja' as mmc %}
{{mmc.hello()}}

We use the jinja import syntax to import the “myMacroCollection.jinja” file in “importExampleTemplate.jinja” file with the alias name mmc. Now we can access all the macros inside mmc with the syntax –

mmc.<macro-name>(<arguments>)

Note how similar this system is to python import syntax. Here are the contents of “importExampleTemplate.py” file which is used to render the “importExampleTemplate.jinja” file and print the output string on the screen –

Python3




from jinja2 import Environment, FileSystemLoader
env = Environment(loader = FileSystemLoader('templates'))
template = env.get_template('importExampleTemplate.jinja')
output = template.render()
print(output)


Output:

Hello!

Conclusion

Jinja is a powerful and popular text rendering engine for Python programming language. It is highly used and integrated by frameworks like Flask and Django. Jinja has many useful and convenient features and one of them is macros. Macros in Jinja are analogous to functions in other programming languages, thus they help in avoiding code redundancy and have clean and manageable codebase inside jinja templates. The syntax to define the arguments and calling the macros is very similar to Python programming language. We have access to these arguments inside the body of macro and we can utilize them in creating the resultant text. The result of executing a macro is always some text. We can call these macros inside the template inside “{{ <some expression> }}” delimiter.



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.

Similar Reads

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) –...

What are Macros in Jinja?

Macros in Jinja are similar to functions in other programming languages. They are used to encapsulate logic used to perform repeatable actions. Macros can take arguments or be used without them. Inside macros, we can use any of the Jinja features and constructs. The result of running a macro is some text. In other words, wherever we call a macro inside the template, it is replaced with the string/text resulting from calling the macro....

Advanced examples of using Jinja Macros

...