I found myself putting `{%load ... %}` in every template that I was writing, so DRY .. I created an app called 'globaltags' and in its `__init__.py`, I just pre-load the tags that I use frequently.
The [pyif](http://www.djangosnippets.org/snippets/130/) and [expr](http://www.djangosnippets.org/snippets/9/) tags are excellent tags, and I highly recommend them for getting the most out of django's template language.
The [dbinfo](http://www.djangosnippets.org/snippets/159/) snippet is something that I came up with to easily output SQL debugging information.
This snippet introduces two tags: `{%dbinfo%}` and `{%dbquerylist%}`. The `{%dbinfo%}` tag returns a string with the # of database queries and aggregate DB time. The `{%dbquerylist%}` tag expands to a set of <LI> elements containing the actual SQL queries executed. If `settings.TEMPLATE_DEBUG` is False, both tags return empty strings.
template tag for producing tag clouds for a given model, using django-tagging application:
http://code.google.com/p/django-tagging/
Sample: http://skam.webfactional.com/tags/
Usage:
{% tags_cloud_for_model articles.Article as object_list %}
{% tags_cloud_for_model articles.Article as object_list step 6 %}
Cheers to limodou for getting me thinking about this. The only problem with his implementation is that it doesn't support Django's "." syntax for accessing array/dict elements. In the Django style of allowing simple syntax for designers while allowing for greater flexibility, and less template duplication for conditionals that were previously impossible to represent in templates, I modified Django's built-in If tag.
This is an adaptation/enhancement to Django's built in IfNode {% if ... %} that combines if ifequal ifnotequal into one and then adds even more. This
Supports
1. ==, !=
2. not ....
3. v in (1,"y",z)
4. <=, <, >=, >
5. nesting (True and (False or (True or False)))
How to use it:
{% pyif i == 1 or (5 >= i and i != 7) and user.first_name in ('John', 'Jacob') %}
'Tis true.
{% else %}
'Tis false.
{% endif %}
I hope you like it.
Simple template tag to show a calendar.
I use it to display events (which is a model with a start_date and end_date attribute.
You probably should change this according to your needs.
Don't repeat yourself: when you wish to have a block of html with a variable value, but only if the variable is set, you can do this:
{% ifvalue company.contact.email as email %}
<h3>Email address</h3>
<a href='mailto:{{ email }}'>{{ email }}</a>
{% endifvalue %}
Instead of this:
{% if company.contact.email %}
<h3>Email address</h3>
<a href='mailto:{{ company.contact.email }}'>{{ company.contact.email }}</a>
{% endifvalue %}
The tags ifvalue and ifnotvalue are provided by this snippet.
If you don't specify `as somename`, then the variable's value will be assigned to the name "value".
Piggybacks on the pagination-related template context variables provided by the `object_list` generic view, adding extra context variables for use in displaying links for a given number of pages adjacent to the current page and determining if the first and last pages are included in the displayed links.
Also makes it easy to implement consistent paging all over your site by implementing your pagination controls in a single place - paginator.html.
Optionally accepts a single argument to specify the number of page links adjacent to the current page to be displayed.
Usage:
`{% paginator %}`
`{% paginator 5 %}`
The loremipsum tag generates random content for use in mockups. It takes this content from Cicero's De finibus bonorum et malorum. By default you get a random number of paragraphs up to 6, but you can affect this behaviour by passing in parameters for quantity and units. Units can be "paragraphs" or "words"; the default is paragraphs. Quantity dictates the number of units returned.
If your django.conf.settings.TEMPLATE_DEBUG is True, then no output is returned, so you should be OK in the event you leave the tag in a production template.
Syntax:
{% loremipsum %}
{% loremipsum quantity="3" units="paragraphs" %}
By leaving the behaviour as random, you can see how your layout handles varying amounts of content. Handy, especially for multi-column pages. [Here's a stoopid site](http://lakes.knoxzilla.com/) where I'm playing with random text, pictures, business listings, etc.
I do plan to add a "corpus" parameter to allow you to put your own corpus between lorumipsum and endloremipsum tags and a template parameter to specify the corpus should come from a template, but this has suited me well thus far.
How to use it
{% pycall os.path.abspath(".") %}
{% pycall os.path.abspath(".") as path %}
This is the {{ path }}.
Syntax
{% pycall module.function(...) [as variable_name] %}
If there is no as variable_name, the result will be output directly.
How to use it
{% pyif i == 1 %}
<p>i=1</p>
{% elif i == 3 %}
<p>i=3</p>
{% else %}
<p>other</p>
{% endif %}
Warning: For now, django don't support elif, so you can use it. And I'v submit a patch about to fix it ( #3090 ). If the patch is accepted, you can use elif tag.
I knew that template in myght template system can receive some parameters just like a function. And I also want to implement this function in django template. So I finish a rough one, the code is pasted here. It just like include, but in order to distinguish with "include" tag, I call it "call". So you can use it:
{% call "some.html" %}
This way just like include tag, and the advanced way:
{% call "some.html" with "a" "b"|capfirst title="title1" %}
{% call "some.html" with "c" "d" title="title2" %}
So you can see, "call" tag can do like a python function, it can receive tuple parameters and key word parameters, just like the function:
def func(*args, **kwargs):pass
How to use it
===============
test_call.html
{% expr "limodou" as name %}
{% call "test/test_sub.html" with "a"|capfirst "b" title="title1" %}<br/>
{% call "test/test_sub.html" with "c" "d" title="title2" %}
expr is also a custom tag written by me. It'll calculate a python expression and save to result to a variable. In this case, the variable it "name".
test_sub.html
{% for i in args %}{{ i }}{% endfor %}
<h2>{{ title }}</h2>
<p>{{ name }}</p>
<h3>args</h3>
{{ args }}
<h3>kwargs</h3>
{{ kwargs }}
And you also can see, call tag will auto create args and kwargs context variables.
I hope this will be some useful.
You can use this tag to "catch" some template snippets and save it into a context variable, then use this variable later.
How to use it
{% catch as var1 %}any tags and html content{% endcatch %}
...
{{ var1 }}
This tag can be used to calculate a python expression, and save it into a template variable which you can reuse later or directly output to template. So if the default django tag can not be suit for your need, you can use it.
How to use it
{% expr "1" as var1 %}
{% expr [0, 1, 2] as var2 %}
{% expr _('Menu') as var3 %}
{% expr var1 + "abc" as var4 %}
...
{{ var1 }}
for 0.2 version
{% expr 3 %}
{% expr "".join(["a", "b", "c"]) %}
Will directly output the result to template
Syntax
{% expr python_expression as variable_name %}
python_expression can be valid python expression, and you can even use _() to translate a string. Expr tag also can used context variables.