Login

Tag "truncate"

Snippet List

Precise truncate chars filter

Template filter that truncates the text when it exceeds a certain number of characters. It deletes the last word only if partial. Adds '...' at the end of the text, only if truncated. Examples (text == 'Lorem ipsum dolor sit amet', len(text) == 26) {{ text|truncatewords_by_chars:30 }} 'Lorem ipsum dolor sit amet' {{ text|truncatewords_by_chars:25 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:21 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:20 }} 'Lorem ipsum dolor...' By Davide Muzzarelli

  • filter
  • template-filter
  • truncate
  • truncatewords
Read More

Truncate text to length up until the nearest space

This will truncate a long character based on the given length parameter. If the word is cut-off, it will return the string up until the next space. If there are no spaces in the next 5 characters, that should mean a very long word and we should truncate right away.

  • template
  • string
  • truncate
Read More

Word-boundary-aware string truncation template filter

This is a custom template filter that allows you to truncate a string to a maximum of num characters, but respecting word boundaries. So, for example, if `string = "This is a test string."`, then `{{ string|truncatechars:12 }}` would give you "This is a..." instead of "This is a te".

  • template
  • filter
  • truncate
  • words
Read More

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

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

Inline truncate by character number

Sometimes I need to truncate a string after a number of characters, usually to avoid breaking the page layout. When the string we have to truncate is a filename I don't want to hide its extension so a user can easily recognize the file. My solution is add the ellipsis at the middle of the string converting `ALongLongLongTitleDocumentThatExemplifiesThisSnippet.txt` into `ALongLongLong...hisSnippet.txt`

  • filter
  • truncate
Read More

Truncate words by characters

This filter truncates words like the original truncate words Django filter, but instead of being based on the number of words, it's based on the number of characters. I found the need for this when building a website where i'd have to show labels on really small text boxes and truncating by words didn't always gave me the best results (and truncating by character is...well...not that elegant). Usage example: {{var|truncatewords_by_chars:"16 2 4"}} If string lenght is higher than 16, truncates by 2 words, if lesser, truncates by 4 words.

  • filter
  • truncate
  • character
  • word
Read More

TruncateChars

**Usuage:** {{ comment.comment|truncatechars:20 }} **Why to use:** First of all, my english isn´t the best. I hope you will understand my text anyways :) Ok, I had a problem with some "kiddies" posting huge comments like "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhh this game rooooooooooocks!!!!!!!!!!!!!!!!". Django has no function to "kill" that huge words within a string. So I´ve coded this little peace and hope that someone could use it. Greets from Germany, Aveal http://www.onlinewars.eu

  • truncate
  • chars
Read More

truncate letters

filter for truncating strings similar to truncatewords only with letters.

  • filter
  • truncate
  • letters
Read More

10 snippets posted so far.