Login

Truncate filter

Author:
zalun
Posted:
April 28, 2009
Language:
HTML/template
Version:
Not specified
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. Bootstrap Accordian by Netplay4 3 years, 7 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 6 years, 7 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 6 years, 7 months ago
  4. Reusable form template with generic view by roldandvg 6 years, 8 months ago
  5. Pagination Django with Boostrap by guilegarcia 6 years, 10 months ago

Comments

Please login first before commenting.