Login

Truncate filter

Author:
zalun
Posted:
April 28, 2009
Language:
HTML/template
Version:
Not specified
Tags:
template filter truncate
Score:
0 (after 0 ratings)

Truncates string to a given number of characters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@register.filter
def trunc( string, number, dots='...'):
  """ 
  truncate the {string} to {number} characters
  print {dots} on the end if truncated

  usage: {{ "some text to be truncated"|trunc:6 }}
  results: some te...
  """
  if not isinstance(string, str): string = str(string)
  if len(string) <= number:
    return string
  return string[0:number]+dots

More like this

  1. truncate letters by trbs 8 years, 2 months ago
  2. truncate by alfor 8 years, 1 month ago
  3. Word-boundary-aware string truncation template filter by josho 6 years ago
  4. Precise truncate chars filter by davmuz 3 years, 4 months ago
  5. Make a string a fixed-width by adamfast 7 years, 11 months ago

Comments

Please login first before commenting.