Improved macros for Jinja
Project description
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 templateregister(name, template): register the specified macro located in the templateregister_from_env(): look for macros in all templates:- by default, only look in templates named
__macros__.html - pass
filter_func=Falseto register from ALL templates - provide a custom
filter_func(will receive the template name as argument)
- by default, only look in templates named
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 itcreate_from_directory(path): create macros from all files in the directory (recursively):- by default, only files with the extension
.macro.html - pass
filter_func=Falseto 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
- by default, only files with the extension
create_from_global(global_name, macro_name): create a macro that calls a context variablecreate_from_func(func): create a macro from a function@env.macros.macrodecorator: 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinja_super_macros-0.2.0.tar.gz.
File metadata
- Download URL: jinja_super_macros-0.2.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c3a50726b350958d9274de13e9ee6c3278147435813574fc8c356849450d1d5
|
|
| MD5 |
16c86b3c8c237323d1151aa976d2b2b6
|
|
| BLAKE2b-256 |
cf919321fcd13a3b95aa67d958f56e4cc17cd6d6afc583f5cee8e3b02370c304
|
File details
Details for the file jinja_super_macros-0.2.0-py3-none-any.whl.
File metadata
- Download URL: jinja_super_macros-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3eebaa98b096acdb37e895197a420380427d9442e4c0e472982b0753ffaeb5fc
|
|
| MD5 |
7d8997650b9484b769f2fef6b993d1d2
|
|
| BLAKE2b-256 |
81c53ea7749e332d59919ee4706b5f7022d4da28e7c9bc4f3b3d33a400e5caf4
|