Support for {% macro %} tags in templates, version 2
Tag library that provides support for *macros* in Django templates. **Usage example:** **0)** Save this file as <yourapp>/templatetags/macros.py **1)** In your template load the library: {% load macros %} **2)** Define a new macro called 'my_macro' with parameter 'arg1': {% macro my_macro arg1 %} Parameter: {{ arg1 }} {% endmacro %}` **3a)** Use the macro with a String parameter: {% usemacro my_macro "String parameter" %} **3b)** or with a variable parameter (provided the context defines 'somearg' variable, e.g. with value "Variable parameter"): {% usemacro my_macro somearg %} **3c)** **!!NEW!!** `usemacro` now also supports filters attached to parameters: {% usemacro my_macro "lowercase parameter"|upper %} Output of the above code would be: Parameter: String parameter Parameter: Variable parameter Parameter: LOWERCASE PARAMETER **4)** **!!NEW!!** Alternatively save your macros in a separate file, e.g. "mymacros.html" and load it into the current template with: {% loadmacros "mymacros.html" %} Macros can take *zero or more arguments* and both context variables and macro arguments are resolved in macro body when used in `{% usemacro ... %}` tag. Bear in mind that Macros are local to each template file and are not inherited through `{% extends ... %}` blocks.
- template
- tag
- macro
- usemacro
- loadmacros