Based on [http://djangosnippets.org/snippets/1615/](http://djangosnippets.org/snippets/1615/)
Added tweets cache. Cache time is configurable through project settings: TWITTER_TIMEOUT=300. Default time is 300 seconds = 5 minutes.
This improve performance by reducing the twitter api queries interval.
Save the code as app_name/templatetags/urlbuilder.py. Supply your view with a dictionary of "default" query params, likely taken from the request. Then, in the template:
`{% load urlbuilder %}
{% url some-url as base_url %}`
That sets you up. Then you can make links. For example:
<th><a href="{% build_url base_url query_params sort=name %}">Name</a></th>
Say query_params has:
page: 2,
filter: active,
sort: id
The above tag would spit out /base/url?page=2&filter=active&sort=name
The tag also has support for using a variable as the replacement key. Say "filter_key" is a variable available to the template with the value "filter":
{% build_url base_url query_params filter_key default %}
Using the above example, that would output /base/url?page=2&filter=default&sort=id
A custom templatetag for inlining image in the browser.
The principe is to base64 encode the image and avoid a http request.
There is a cache handling, you just have to specify a writable directory.
An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/)
The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)
The example of the Image inlining template tag lib which inline images in the browser instead of making an Http request.
The lib is available at : [http://djangosnippets.org/snippets/2268/](http://djangosnippets.org/snippets/2268/)
Enhanced version of snippet [1113](http://djangosnippets.org/snippets/1113/)
Usage example (not self-contained):
@register.tag
def groupurl(parser, token):
'''
Syntax::
{% groupurl view_name group [key=val, [key=val, ...]] [as varname] %}
Example::
<a href="{% groupurl blog_detail group slug=blog.slug %}">{{ blog.name }}</a>
'''
bits = token.contents.split()
tag_name = bits[0]
if len(bits) < 3:
raise template.TemplateSyntaxError("'%s' takes tag at least 2 arguments (path to a view and a group)" % (tag_name,)
bits_iter = iter(bits[1:])
# view_name + group and url kwargs
args, kwargs = parse_args_and_kwargs(parser, bits_iter, stop_test='as', tagname=tag_name)
if len(args) != 2:
raise template.TemplateSyntaxError("'%s' takes exactly two non-kwargs (path to a view and a group)" % (tag_name,))
view_name, group = args
# as var
asvar = None
for bit in bits_iter:
asvar = bit
return GroupURLNode(view_name, group, kwargs, asvar)
Put this snippet in a file in a templatetags/ directory and load it in your templates. Then use it to encode your emails:
`{{"[email protected]"|html_encode_email}}`
Or if you want some control over the anchor tag:
`<a href="mailto:{{"[email protected]"|html_encode}}&subject=Feedback">Send Feedback</a>`
From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)
You can convert any string to a QR code image as easy as use a simple filter, thanks to google charts api.
Common use:
<img src="{{object.attribute_to_encode|qr:"120x130"}}" />
This will create a 120(width) x 130(heiht) image with the value of the attribute_to_encode encoded in a QR coded image.
This is in the spirit of php's include_once or a C preprocessor #ifndef. Kind of. As you might've guessed, the output of
{% renderonce %}foo{% endrenderonce %}
{% renderonce %}foo{% endrenderonce %}
in a template will be a single 'foo'. I use it for js script tags personally, to prevent duplicate inclusions. If you ended up here, chances are you've already explored the "use inheritance" or "use django-(app X)" solutions, so feel free to omit such comments.
This is the `local_settings.py` trick extended to Django templates.
Sometimes you need to insert some arbitrary code in the HTML of the production site for external service integration like uservoice, typekit, google analytics... You don't want to put this code into source control because some other sites using the same source code may not need it.
So, add this template tag to your collection and do:
{% try_to_include 'head.html' %}
And leave `head.html` out of source control. Then when you need to include some code on your production site, just add the `head.html` template with the desired code to include.
I usually have one included template in the header for extra `<head>` tags, and one in the footer for extra javascript.
Node that the included template is rendered against the current context. If the template doesn't exist, an empty string is returned.
Also see the [full blog post](http://bruno.im/2009/dec/07/silently-failing-include-tag-in-django/) about this tag.
A simple decorator to register a template.Node object as a tag, without the need to write the usual "`token.split_contents()` yatta yatta blatta blatta" function.
The decorator should be called with the name of the tag, and an optional `required` named argument in case your tag requires a minimum number of arguments.
Please note that all the arguments in the templatetag call will be passed to the node (except the templatetag name and the `for` and `as` keywords) in the order given.
Suggestions etc are very welcome, of course =)
This is the same as [{% widthratio %}](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio) but when the given value is greater than the max_value it just uses the max_value. This way the result is never greater than max_value. It also returns max_width if the max_value is 0 instead of returning a blank string. Usage example: {% smart_widthratio this_value max_value 100 %}
Based on http://www.djangosnippets.org/snippets/592/, a simplified recurse template tag that will explore dict and list objects. Useful and straightforward for JSON documents (for instance from couchdb :p).
Actual usage example:
{% recursedict mydictionary %}
<ul>
{% loop %}
<li>{% if key %}<b>{{ key }}</b>: {% endif %}{% value %}</li>
{% endloop %}
</ul>
{% endrecurse %}
The main tag syntax is "recursedict var" where var is your dictionary name.
The "key" property will contain the current key, or None if the current value comes from a list (in other words the list (a, b, c) is handled like a very hypothetical {None: a, None: b, None: c} dictionary.)
{% value %} will output the current value, or recurse if the value is a list, tuple or dict.