Login

Tag "templatetag"

Snippet List

Template tag "ifregex" and "ifnotregex"

Outputs the contents of the block if the second argument matches (or not, depending on the tag) the regular expression represented by the first argument. Usage: {% ifregex "^/comments/" request.path %} ... {% endifregex %} {% ifnotregex "^/comments/" request.path %} ... {% else %} ... {% endifnotregex %}

  • template
  • templatetag
  • regex
  • ifregex
Read More

Django Breadcrumbs Snippet

A simple tag to render breadcrumbs. Usage: {% load breadcrumbs %} {% breadcrumbs "['Home','Home','home']" "['Articles','Articles','articles']" "['object','object','object.get_absolute_url']" %} Loads up the template in "modules/breadcrumbs.html" and renders it with a list of items. You can provide the tag either with plain text stuff and named urls as the third argument ( any more arguments per bracket-block is parsed as args / kwargs for the reverse() call ) or the object directly, and the script tries after failing the reverse() to resolve the provided arguments. Have pun.

  • django
  • templatetag
  • breadcrumbs
Read More

ImageMagick commands from Django Templates

A template filter which wraps imagemagick's `convert` command. The filter acts upon a source image path, and returns the filtered image path. usage: {{ source_path|convert:"-resize 64x64\!" }} The filter parameter is the processing arguments for an ImageMagick 'convert' command. See e.g. http://www.imagemagick.org/Usage/resize/ Every image created is saved in a cache folder. This code does not handle removing obsolete cached images. If the filtered image path exists already, no image processing is carried out, and the path is returned.

  • filter
  • image
  • templatetag
  • imagemagick
Read More

Page numbers with ... like in Digg

Digg-like page numbering using inclusion tag. Usage in template: {% load pagination %} {% pagination yourpage %} Inclusion template `pagination.html`: {% load i18n %} <div class="pagination"> <span class="step-links"> {% if page.has_previous %} <a href="?page={{ page.previous_page_number }}" class="previous">{% trans "previous" %}</a> {% endif %} {% for pnum in begin %} {% ifequal page.number pnum %} <span class="current">{{ pnum }}</span> {% else %} <a href="?page={{ pnum }}">{{ pnum }}</a> {% endifequal %} {% endfor %} {% if middle %} <span class="continue">...</span> {% for pnum in middle %} {% ifequal page.number pnum %} <span class="current">{{ pnum }}</span> {% else %} <a href="?page={{ pnum }}">{{ pnum }}</a> {% endifequal %} {% endfor %} {% endif %} {% if end %} <span class="continue">...</span> {% for pnum in end %} {% ifequal page.number pnum %} <span class="current">{{ pnum }}</span> {% else %} <a href="?page={{ pnum }}">{{ pnum }}</a> {% endifequal %} {% endfor %} {% endif %} {% if page.has_next %} <a href="?page={{ page.next_page_number }}" class="next">{% trans "next" %}</a> {% endif %} </span> </div> Produces: previous 1 2 ... 4 5 6 7 **8** 9 10 11 12 ... 17 18 next Or: **1** 2 3 4 5 6 7 8 ... 17 18 next Or: previous 1 2 ... 10 11 12 13 14 15 16 **17** 18 next

  • tag
  • django
  • templatetag
  • pagination
  • digg
  • pages
Read More

Naked CSS Day TemplateTag

This TemplateTag simply returns a True when it is Naked CSS Day allowing you to hide your CSS or display a custom message on that date. Allows allows for parameters should the date change (again).

  • templatetag
  • css
  • nakedcss
  • naked
Read More

Language-aware template inclusion

Looks up for a template based on the template-name plus the current users language code. Loads the template and renders it with the current context. Example:: {% langinclude "foo/some_include.html" %} Based on the users LANGUAGE_CODE, assumed we have 'de', it tries to render the template 'foo/some_include.html.de'. If that doesn't exists, it renders the template 'foo/some_include.html'. This is the default behavior of the include-Tag. Basically this is a shortcut for the following code, just with a fallback for the default template:: {% ifequal LANGUAGE_CODE "de" %} {% include "foo/some_include.html.de" %} {% else %} {% include "foo/some_include.html" %} {% endifequal %} --- Ein deutscher [Weblogeintrag mit Beschreibung](http://www.mahner.org/weblog/sprachabhangige-template-imports/)

  • templatetag
  • i18n
  • language
  • include
Read More

Uk postcode googlemap templetag

Entirely based on and with big thanks to: [http://www.tomanthony.co.uk/](http://www.tomanthony.co.uk/) Drops in a googlemap with a placemarker based on a uk postcode Looks like this: {% googlemap_from_ukpostcode postcode "XxY" zoom %} e.g. {% googlemap_from_ukpostcode "SP27AS" "220x290" 16 %} postcode and zoom can optionally be template variables. "XxY" is the x/y size of the map you want to drop in. zoom can be omitted and defaults to 14. requires: in settings: GOOGLE_AJAX_API_KEY, GOOGLE_MAPS_API_KEY google_map_ukpostcodes.js: is slight variation on js at http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js For further and better info see: [http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/](http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/)

  • templatetag
  • googlemap
  • postcode
Read More

getting the related entries with a templatetag using django-tagging

Django tagging by default doesn't provide a templatetag to get the related objects for another object. Even though this is implemented as a model. Still, one can use the existing templatetags to achieve the same outcome. Of course, writing a custom templatetag would be more efficient in terms of database access.

  • templatetag
  • tagging
  • related
  • django-tagging
Read More
Author: V
  • 0
  • 2

More flexible "Partial Template"

This is an extension of the snippet http://www.djangosnippets.org/snippets/1302/ to make it a bit more flexible and been able pass more than one parameter to our "Partial Template". To use it you can {% partial_template template_name param1:variable1 param2:variable2 ... %} or: {% partial_template partials/mini_template.html item:data1 var2:"False" var3:"2*2" %}

  • template
  • templatetag
  • include
Read More

Truncate string after a given number of chars keeping whole words

Truncates a string after a given length, keeping the last word complete. This filter is more precise than the default `truncatewords` filter. Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense. There is a [blog post](http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/) about this snippet (in Portuguese).

  • template
  • filter
  • templatetag
  • truncate
  • templatetags
  • words
Read More
Author: rix
  • 5
  • 6

Boolean Image Flag TemplateTag

Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following: `{% boolean_img user.is_active %}` And get the following output: `<img src="/media/icons/accept.png" alt="True" />` All you need to do is use the custom templatetag code, load it in your template and use the `boolean_img` tag. **Adjust templates, html and images where needed**

  • image
  • templatetag
  • boolean
  • flag
Read More

A GET string modifier templatetag

This template tag takes the current GET query, and modifies or adds the value you specify. This is great for GET-query-driven views, where you want to provide URLs which reconfigure the view somehow. **Example Usage:** `{% get_string "sort_by" "date" %}` returns `all=your&current=get&variables=plus&sort_by=date`

  • templatetag
  • get
  • context
  • request
Read More

Pygments Syntax highlighting template tag

This template tag will attempt to apply pygments syntax highlighting to anything inside {% code %} and {% endcode %} tags. You can optionally pass in the language to highlight in the form of {% code 'lang' %} - if you don't, it will first try to guess the syntax, and then fall back to Python syntax highlighting.

  • templatetag
  • pygments
  • highlighting
  • syntax
Read More

118 snippets posted so far.