Add macros to your django templates
Project description
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:
Github repository: https://github.com/twidi/django-templates-macros
Pypi package: https://pypi-hypernode.com/pypi/django-templates-macros
Usage
Add this app “macros” to your INSTALLED_APPS
In your template load the library:
{% load macros %}
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 %}
Use the macro with string parameters or context variables:
{% usemacro my_macro "foo" "bar" baz="KW" %} <br> {% usemacro my_macro num_pages "bar" %} <br> {% setmacro my_macro %} {{ my_macro }}
Renders like:
foo bar KW 77 bar Default baz default_arg1 default_arg2 Default baz
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 | 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
File details
Details for the file django-templates-macros-0.2.tar.gz
.
File metadata
- Download URL: django-templates-macros-0.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | add5e41e01be17c56acc470265afefce110bd1e249e36af194ab7481d451ac86 |
|
MD5 | 1d6e09caebdac03709ea0f17c0e4017d |
|
BLAKE2b-256 | 2dbb67e4dfff1d150c88bd185606068225169d148cc2e99dd6ac5a5d0a068e39 |
File details
Details for the file django_templates_macros-0.2-py2-none-any.whl
.
File metadata
- Download URL: django_templates_macros-0.2-py2-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a810c9e5983bf8cb301166d051f0eacf616582cfdf400cd657f08f5551fdfa89 |
|
MD5 | 8fc6961fec7ce23707f7fed024768214 |
|
BLAKE2b-256 | 450e9f8f802d3549f6fdfcf667d20e8bd68ba26f6d0b7131a02d31f3b6920311 |