Snippet List
Disclaimer: I'm not the world's greatest programmer, so there may be better ways to do this, but it works for me (feel free to offer your improvements, though!).
Basically, this will pad an integer with leading zeros and return a string representation. User it like this:
{% forloop.counter|leading_zeros:"5" %}
...where "5" is the number of desired digits. In this case, if it was the 12th time through the forloop, the filter would return "00012".
Why do this? Either for alignment, such as in tables, or for aesthetics -- for an example, see [Shaun Inman's comment section](http://shauninman.com/archive/2007/11/16/mobilesafari_view_source).
This is a simple filter I use to display a list of links from a blog entry off in the sidebar ([example](http://www2.jeffcroft.com/blog/2007/feb/25/two-new-django-sites-both-source-available/)).
Requires beautifulsoup. Originally by [Nathan Borror](http://playgroundblues.com), tweaked slightly by me.
- template
- filter
- beautifulsoup
The {% widthratio %} template tag is under appreciated! Here, it's combined with CSS to create a bar graphic for the results of an election (this example comes from [this page](http://flickr.com/photos/postneo/405239750/in/photostream/), but has been modified slightly for simplicity's sake).
The widthratio tag can be used to create all sorts of graphs and charts, as well as things like tag clouds. Here, we pass it the number of votes for a candidate, the total number of votes in the election, and the integer 190, which is the width, in pixels, of a "full" bar on the bar graph. In other words, 100% = 190 pixels.
It works great!
- template
- templatetag
- widthratio
- graph
- infographic
This is just a very short (and mostly useless on it's own) example of how the built in slugify filter can be used in a Python script to generate slugs. It was pulled from a script I've written to pull in items from Upcoming.org's API.
I post it because "sunturi" posted [Snippet #29](http://www.djangosnippets.org/snippets/29/), which duplicates the functionality of the built-in slugify filter. In the comments of that snippet, santuri (and others) seem to believe that template filters can only be used within templates. This is incorrect, and I think it's important people understand they can be used elsewhere.
sunturi's snippet does remove prepositions from values before slugifying them, so if you need that, his code will work work. But if all you need is slugification, the built-in slugify filter will work fine -- in a Python script, as well as in a template.
- template
- filter
- django
- slug
- built-in
**Support good typography! Avoid widows! **
"Widows" are single words that end up on their own line, thanks to automatic line-breaks. This is an no-no in graphic design, and is especially unsightly in headers and other short bursts of text. This filter automatically replaces the space before the last word of the passed value with a non-breaking space, ensuring there is always at least two words on any given line. Usage is like so:
{{ blog.entry.headline|widont }}
It's a simple tag, but good typography is one of the hallmarks of a well-designed site that separates it from amateurish counterparts.
Note: The idea and name "widont" is copped directly from [Shaun Inman](http://shauninman.com), who wrote a [similar feature for WordPress](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin).
- filter
- widows
- templatetag
- typography
- shauninman
- widont
jcroft has posted 5 snippets.