Skip to main content

PyPI Version

django-templates-macros

Add macros to your django templates

Origin

The original code of this app is taken from https://djangosnippets.org/snippets/2892/ by MattP.

This snippets cites:

Based on snippet by
    Michal Ludvig <michal@logix.cz> http://www.logix.cz/michal
    http://djangosnippets.org/snippets/363/

Extended for args and kwargs into templatetags/kwacro.py by
    Skylar Saveland <michal@logix.cz> http://skyl.org
    https://gist.github.com/skyl/1715202

Modified to support rendering into context by matt@peloquin.com

Where

You can find this package here:

Usage

  1. Add this app “macros” to your INSTALLED_APPS

  2. In your template load the library:

    {% load macros %}
  3. Define a new macro called ‘my_macro’ that takes args and/or kwargs All will be optional:

    {% macro my_macro arg1 arg2 baz="Default baz" %}
        {% firstof arg1 "default_arg1" %}
        {% if arg2 %}{{ arg2 }}{% else %}default_arg2{% endif %}
        {{ baz }}
    {% endmacro %}
  4. Use the macro with string parameters or context variables:

    {% usemacro my_macro "foo" "bar" baz="KW" %}
    <br>
    {% usemacro my_macro num_pages "bar" %}

Renders like:

foo bar KW
77 bar Default baz
  1. Alternatively save your macros in a separate file, e.g. “mymacro.html” and load it to the current template with:

        {% loadmacros "mymacros.html" %}
    
    Then use these loaded macros in as described above.

Bear in mind that defined and loaded macros are local to each template file and are not inherited through {% extends … %} tags.

  1. When recursive macros are needed, use the ‘recurse_macro’ template tag:

    {% macro MENU entries %}
    <ul>
        {% for entry in entries %}
        <li>
            <a href="{{ entry.link }}"> {{ entry.label }} </a>
            {% if entry.children %}
                {% recurse_macro MENU entry.children %}
            {% endif %}
        </li>
        {% endfor %}
    </ul>
    {% endmacro %}
    {% usemacro MENU menu.children %}

Release files for django-templates-macros 0.3

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

Source distribution (sdist)

Source distribution for django-templates-macros 0.3
File Size Uploaded
django-templates-macros-0.3.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-templates-macros 0.3
File Interpreter ABI Platform
django_templates_macros-0.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / django-templates-macros-0.3.tar.gz

Download URL django-templates-macros-0.3.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d67e05b014201b8512eec2e106a9341a49e7691b7b83e6679b40ee9630a2e80e
BLAKE2b-256 checksum
How to use checksums
c75c41cfe5b1a49df4f9b64fb9ca9aac4362faa079665af5bfaf399985f09eac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Release files / django_templates_macros-0.3-py3-none-any.whl

Download URL django_templates_macros-0.3-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aadee18b13086c98860a870716f4c0b134eeadb4f89f626ef074e1872ff41ea7
BLAKE2b-256 checksum
How to use checksums
d9a6248cc57d4717e2bf9c9ff36d745a14da7d42d16099a60d22a590c2552607
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.3 This release

2 release files

0.2

2 release files

0.1

1 release file

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