Login

Tag "macro"

Snippet List

Template {% macro %} support, with context rendering

Reuse blocks of template code and content as macros. This is a small extension of https://gist.github.com/skyl/1715202 (which was based on http://djangosnippets.org/snippets/363/) to support rendering macro output into context variables. See comments for details.

  • template
  • tag
  • macro
Read More

icon shortcut - pseudohtml tag with attribute merging and variables resolving

A shortcut for generating img-s with predefined classes and attributes that mimics a html tag, while resolving context variables inside {% %} without crutches like tag/stuff/endtag. Used as `{% icon test class="spam" eggs="{{ object.pk }}" %}` yields `<img src="http://host.tld/media/icons/test.png" alt="" class="icon16 spam" eggs="42"/>`. Not customizable here for simplicity.

  • html
  • macro
Read More
Author: wiz
  • 0
  • 0

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
Read More

Repeat blocks with new context / simple Jinja-like macro system

A simple macro system that makes it possible to reuse previously defined blocks, optionally with a custom context, similar to the macro functionality in Jinja. It requires some workarounds/hacks because we cannot reach all the data from inside the django template system that we need, but it seems to work pretty well so far. It is, however, also pretty untested at this point, so use at your own risk. Examples: base.html: <!-- This is mandatory if you want to use the repeat-tag in a template. It should as placed as earily as possible. See below for how to mix with template inheritance. --> {% enablemacros %} <!-- Note that {{ param }} does not exist. --> {% block foo %} A standard django block that will be written to the output. {% if param %}{{ param }}{% endif %} {% endblock %} {% macro bar %} Pretty much the same thing as a django block (can even be overridden via template inheritance), but it's content will NOT be rendered per default. Please note that it ends with ENDBLOCK! {% if param %}{{ param }}{% endif %} {% endblock %} <!-- Render foo for the second time --> {% repeat foo %} <!-- Render foo bar the first time --> {% repeat bar %} <!-- Render both blocks again, and pass a parameter --> {% repeat foo with "Hello World" as param %} {% repeat bar with "Hello World" as param %} {% macro form %}do stuff with: {{ form }}{% endblock %} {% for form in all_forms %} {% repeat display %} <!-- will have access to {{ form }} {% endfor %} extend.html: <!-- {% extends %} requires that it be the first thing in a template, and if it is, everything except for block tags is ignored, so {% enablemacros %} won't work. Instead, use: --> {% extends_with_macros 'base.html' %} {% block foo %} Will override "foo" in base.html {% endblock %} {% block bar %} Will override the macro block "bar" in base.html. Whether this is defined as block or macro doesn't matter. {% endblock %} Todo: * This (both tags used) results in infinite recursion: {% extends_with_macros "somefile" %}{% enablemacros %}

  • templates
  • macro
  • jinja
  • repeat
  • reuse
  • variables
Read More

4 snippets posted so far.