Skip to main content

Add macros to your django templates

Project description

PyPI Version


Add macros to your django templates


The original code of this app is taken from by MattP.

This snippets cites:

Based on snippet by
    Michal Ludvig <>

Extended for args and kwargs into templatetags/ by
    Skylar Saveland <>

Modified to support rendering into context by


You can find this package here:


  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" %}
    {% usemacro my_macro num_pages "bar" %}
    {% setmacro my_macro %} {{ my_macro }}

Renders like:

foo bar KW
77 bar Default baz
default_arg1 default_arg2 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.

Project details

Release history Release notifications

This version
History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django_templates_macros-0.2-py2-none-any.whl (6.0 kB) Copy SHA256 hash SHA256 Wheel 2.7 Dec 26, 2016
django-templates-macros-0.2.tar.gz (4.7 kB) Copy SHA256 hash SHA256 Source None Dec 26, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page