Login

Snippets by olau

Snippet List

Truncate HTML without breaking tags

Put it in appname/templatetags/truncatehtml.py and load it with {% load truncatehtml %}, then for instance {{ some_story|truncatehtml:100 }} to truncate the story to 100 characters. Tags are not counted in the length given, and character entities count as one character. The filter should never break an open-tag text close-tag sequence without adding in the close tag. It will also preserve character entities. It won't sanitize the HTML, though: garbage in, garbage out. There's a bit more info about how it works in a [blog post](http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html) I wrote.

  • template
  • filter
  • truncate
  • html
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

olau has posted 2 snippets.