Truncate filter

 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 4 years, 10 months ago
  2. truncate by alfor 4 years, 10 months ago
  3. Word-boundary-aware string truncation template filter by josho 2 years, 8 months ago
  4. Precise truncate chars filter by davmuz 2 weeks, 6 days ago
  5. truncatechars filter by semente 2 years, 5 months ago

Comments

(Forgotten your password?)