Login

Tag "templatetag"

Snippet List

Pygmentation

Usage: (if you save it as pigmentation.py as I did) {% load pigmentation %} {% autoescape off %} {{ somevariable|pygmentize }} {% endautoescape %} There already a few of this code around, but this one is pretty clean, and includes css. It also works in both the development server and Dreamhost (python2.4 in my django config) without any unicode problems.

  • filter
  • tag
  • templatetag
  • pygments
  • code
  • colorize
  • color
Read More

hide_email

This templatetag obsfucate mailto link and hide it while not happen onmouseover event. Usage: {% hide_email object.author object.author.email %} Output: <a href="mailto:[email protected]" onmouseover="var a=String.fromCharCode(79+35,14+103,66+41,30+71,49+49,16+81); var b=String.fromCharCode(14+50,9+105,61+56,81+26,92+9,2+96,20+77,13+33,75+24,32+79,31+78); this.href=['mail','to:',a,b].join('');">rukeba</a> Based on [Email Obsfucator](http://www.djangosnippets.org/snippets/536/) and [forums at Yandex.Market](http://market.yandex.ru/forums/). Example at [my guitar blog](http://rukeba.com/ra/2008/01/15/oasis-wonderwall/)

  • templatetag
  • email
  • antispam
Read More

Simple e-mail template tag

Usage: {% mailto email [linktext] %} `email' parameter is required; linktext is optional, defaulting to the email address.

  • template
  • tag
  • templatetag
  • email
Read More

Customizable newforms labels with a template tag

This template tag and suggested template allow greater control over rendering `<label>` tags for your newforms fields than using `field.label_tag`. I save the provided Python code in my app as `templatetags/forms.py` (although this name may conflict in the future). The simplest usage: {% label field %} One use case is adding `class="required"` to the label tag for required fields instead of inserting markup elsewhere--this is done in the given example template. Alternate label text and tag attributes can be passed to the inclusion tag: {% label field "Alt. label" 'class=one,id=mylabel' %}

  • templatetag
  • newforms
  • label
Read More

Paginator TemplateTag

**Paginator TemplateTag** TemplateTag to use the new Paginator class directly from a template. The paginate template tags take the following options: 1. list or queryset to paginate 2. number of pages 3. [optionaly] name of the Paginator.Page instance; prefixed by keyword 'as' 4. [optionaly] name of the http parameter used for paging; prefixed by keyword 'using' If you want to specify the parameter name with the keyword 'using' you must use the 'as' keyword as well. The default name of the paging variable is "page" and the paginator (the class that knows about all the pages is set in the context as "page_set". This follows the naming scheme of the ORM mapper for relational objects where "_set" is appended behind the variable name. Usage, put the following in your template: {% load paginate %} {% get_blog_posts blog_category as posts %} {% paginate posts 10 as page using page %} <ul> {% for post in page.object_list %} <li>{{ post.title }}</li> {% endfor %} </ul> <div> {% if page.has_previous %} <a href="?page={{ page.previous_page_number }}">previous</a> {% endif %} <i>{{ page.number }} of {{ page_set.num_pages }}</i> {% if page.has_next %} <a href="?page={{ page.next_page_number }}">next</a> {% endif %} </div> The templatetag requires the request object to be present in the template context. This means that you need 'django.core.context_processors.request' added to settings.TEMPLATE_CONTEXT_PROCESSORS list or otherwise make sure that the templatetag can access the request object. Comments are appreciated.

  • templatetag
  • pagination
  • paginator
Read More

TemplateTag to Split a List into Uniform Chunks

Creates a template tag called "split_list" which can split a list into chunks of a given size. For instance, if you have a list 'some_data', and you want to put it into a table with three items per row, you could use this tag: {% split_list some_data as chunked_data 3 %} Given a some_data of [1,2,3,4,5,6], the context variable 'chunked_data' becomes [[1,2,3],[4,5,6]] This is useful for creating rows of equal numbers of items. Thanks to the users of #django (pauladamsmith, Adam_G, and ubernostrum) for advice and pointers, thanks to Guyon Mor&eacute;e for writing the chunking recipe that this tag is based on.

  • templatetag
  • split
  • list
Read More

Recurse template tag for Django

[Original and further information available from here.](http://www.undefinedfire.com/articles/recursion-in-django-templates/) **v1.1 Update (20/04/08):** Added the ability to recurse single elements as well as automatic skipping of empty elements. Most of the tags are self explanatory, the only one that may cause confusion is the main `{% recurse %}` one. The format for this tag is `{% recurse [children] with [parent] as [child] %}` where “[children]” is the property that contains the children of the current element, “[parent]” is your starting element and “[child]” is the variable named used in the loop. Example usage: {% load recurse %} ... Headers and stuff ... {% recurse category.category_set.all with categories as category %} <ul> {% loop %} <li> <h{{ level }}>{{ category.title }}</h{{ level }}> {% child %} </li> {% endloop %} </ul> {% endrecurse %} ... The rest of the page ...

  • template
  • templatetag
  • recursion
Read More

Regrouping admin models

This is modification of Django's original adminapplist template tag. You can move your models from one app to other or completely hide them with this mod. Copy django_dir/contrib/admin/templates/admin/index.html file to your templates/admin folder, open it, then change {% load adminapplist %} to {% load custom_adminapplist %} (or whatever you named the templatetag file) After that, write your regrouping schema to settings.py file like this; UPDATED, now using tupples instead of dicts in APP_SCHEMA to make it more DRY. ` APP_SCHEMA=[ ( ['Model','List'], 'From App', 'To App', ), ( ['FlatPage'], 'Flatpages', 'Site Content', ), ( ['Product'] 'Product', 'Shop', ), ( ['Site'] 'Sites', #We are hiding Site model by not defining a target. ), ] `

  • templatetag
  • models
  • admin
  • app
Read More

Template tags/filter for working with query strings

Some template tags/filter for working with query strings in templates. Examples: {% load qstring %} {% qstring %} # Prints current request's query string {% qstring as current_qstring %} # Same but goes to context {{ current_qstring|qstring_del:"key1" }} # Deletes all key1 values {{ current_qstring|qstring_del:"key1&key2" }} # Deletes all key1 and key2values {{ current_qstring|qstring_set:"key1=1&key2=2" }} # Deletes all old key1 and key2 values and adds the new values.

  • templatetag
  • string
  • query
Read More

Hyperlink list filter

This is a simple filter that takes an object list and returns a comma-separated list of hyperlinks. I use it to display the list of tags in my blog entries. The function assumes the object has a `get_absolute_url()` method that returns the URL. In your template, write `{{ obj_list|hyperlink_list:"slug" }}` where `obj_list` is your object list and `"slug"` is the object's attribute that returns the link text.

  • filter
  • templatetag
  • links
  • template-tag
  • link
  • hyperlink
Read More

packjs templatetag

Based on jspacker by Dean Edwards, Python port by Florian Schulze: http://www.crowproductions.de/repos/main/public/packer/jspacker.py Packs javascript Example usage:: {% packjs %} var a = 1; var b = 2; var c = 3; alert(a+b); {% endpackjs %} This example would return this script:: eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp("\\b"+e(c)+"\\b","g"),k[c]);return p}('0 5 = 1;\n 0 4 = 2;\n 0 7 = 3;\n 6(5+4);\n',8,8,'var||||b|a|alert|c'.split('|'),0,{}))

  • templatetag
  • javascript
  • pack
Read More

ifsecure

In some cases we need to know if we were opened via https from template. Usage: {% ifsecure %}using https{% else %}not using https{% endifsecure %} If you use fastcgi fastcgi_param HTTPS must exists.

  • templatetag
  • is_secure
  • https
Read More

blocksetter

Sometimes you need to set a little bit more complex variable in the template (e.g. Title), that is being used more than once. this simple tag defines "blockset" tag. {% blockset variable-name %}{%endblockset} everything inside body (between blockset/endblockset) is being assigned to the variable "variable-name". I have found this quite useful with translations, or setting the title, where you out several variables into one sentence. Drop me an email if you will find this useful.

  • templatetag
  • block
  • set
Read More

Filter for adding quote marks

A template filter for adding curly quotes around a string. The filter understands enough HTML to put the quotes inside an initial paragraph begin and ending paragraph end, if they exist. Put the code inside a file in a templatetags subdir in your app, add a {% load myfile %} statement and you're ready to go with {{somebodysays|addquotes}}. Of course, beware the script kiddies, be careful with escaping.

  • templatetag
  • quotes
Read More

118 snippets posted so far.