Skip to main content

Jinja Super Macros

Jinja Super Macros introduces a new syntax to call macros as well as an automatic macro loader.

{% macro Form(action) %}
    <form action="{{ action }}" method="post">
        {{ caller() }}
    </form>
{% endmacro %}

{% macro FormInput(name) %}
    <input type="text" name="{{ name }}">
{% endmacro %}

<{Form action="/" }>
    <{FormInput name="email" }/>
</{Form}>

Installation

pip install jinja-super-macros

Usage

To start using super macros, configure the environment with additional extensions and settings and register your macros:

from jinja2 import Environment, PackageLoader
from jinja_super_macros import configure_environment

env = Environment(loader=PackageLoader(__name__, 'templates'))
configure_environment(env)

env.macros.register_from_template("macros.html")

You can register macros from templates available through your loader:

  • register_from_template(template): register all macros defined in the template
  • register(name, template): register the specified macro located in the template
  • register_from_env(): look for macros in all templates:
    • by default, only look in templates named __macros__.html
    • pass filter_func=False to register from ALL templates
    • provide a custom filter_func (will receive the template name as argument)

You can also register macros using templates not accessible from your environment loader:

  • register_file(filename)
  • register_directory(path)
  • register_package(package_name, package_path='macros')

New macro tags syntax

Macro tags allow you to use Jinja's macros with a syntax similar to html tags. There are two types of macro tags: inline and block. Inline is the equivalent of calling the macro using {{ macro_name() }} and block is equivalent to the {% call %} directive.

Inline directives are enclosed in <{ and }/>. Arguments can be provided the same was as html attributes but their values are Jinja expressions.

Inline tag example:

<{macro_name arg1=value1 arg2=value2 arg-with-dashes=value3 }/>

is equivalent to:

{{ macro_name(arg1=value1, arg2=value2, arg_with_dashes=value3) }}

Block tags, start with an opening directive enclosed in <{ and }> and must be closed with a closing directive </{macro_name}> (note that the macro name is optional in the closing directive).

Block tag example:

<{macro_name arg1=value1 arg2=value2 }>
    my macro content
</{macro_name}>

is equivalent to:

{% call macro_name(arg1=value1, arg2=value2) %}
    my macro content
{% endcall %}

The list of attributes acts almost the same as a function call but with spaces instead of comma. This means that values can be single expressions with no operators or a full expression enclosed in parentheses.

<{macro_name arg1 arg2 kw-arg1=single_value kw-arg2=("a" if True else "b") **kwargs }/>

By default, when a macro tag is used but no macro is found matching the name, it will fallback to rendering the html tag:

<{input type="checkbox" checked=is_checked() }/>

New ways to define macros

Jinja Super Macros registry's also gives you the possibility to create macros from new sources.

  • create_from_file(filename): wrap the content of the file in a macro directive and register it
  • create_from_directory(path): create macros from all files in the directory (recursively):
    • by default, only files with the extension .macro.html
    • pass filter_func=False to register fromm ALL files
    • provide a custom filter_func (will receive the template name as argument)
    • files starting with a dot or un underscore are always ignored
  • create_from_global(global_name, macro_name): create a macro that calls a context variable
  • create_from_func(func): create a macro from a function
  • @env.macros.macro decorator: create using the decorated function

When creating from files, use kwargs.something to access keyword arguments passed to the macro.
Use caller() as usual for inner content.

When creating from function, pass receive_caller=True to receive the caller argument.

Metadata

Release files for jinja-super-macros 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja-super-macros 0.2.0
File Size Uploaded
jinja_super_macros-0.2.0.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja-super-macros 0.2.0
File Interpreter ABI Platform
jinja_super_macros-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / jinja_super_macros-0.2.0.tar.gz

Download URL jinja_super_macros-0.2.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2c3a50726b350958d9274de13e9ee6c3278147435813574fc8c356849450d1d5
BLAKE2b-256 checksum
How to use checksums
cf919321fcd13a3b95aa67d958f56e4cc17cd6d6afc583f5cee8e3b02370c304
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / jinja_super_macros-0.2.0-py3-none-any.whl

Download URL jinja_super_macros-0.2.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3eebaa98b096acdb37e895197a420380427d9442e4c0e472982b0753ffaeb5fc
BLAKE2b-256 checksum
How to use checksums
81c53ea7749e332d59919ee4706b5f7022d4da28e7c9bc4f3b3d33a400e5caf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page