1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from django import template
register = template.Library()
@register.filter
def truncatechars(s, num):
"""
Truncates a word after a given number of chars
Argument: Number of chars to truncate after
"""
length = int(num)
string = []
for word in s.split():
if len(word) > length:
string.append(word[:length]+'...')
else:
string.append(word)
return u' '.join(string)
|
More like this
- truncatechars filter by semente 3 years, 8 months ago
- Dynamic Test Loading by cronosa 3 years, 4 months ago
- LanguageField by marinho 5 years, 5 months ago
- CustomChoiceField, Selectable label field version of ModelChoiceField by mauro 5 years, 2 months ago
- Newforms field for decimals with a comma by jonasvp 5 years, 2 months ago
Comments
{{ comment.comment|slice:":20" }} ?
#
@daevaorn
example: comment.comment = soooooooooome huuuuuuuge word
{{ comment.comment|slice:":5" }} = soooo
{{ comment.comment|truncatechars:5 }} = soooo... huuuu... word
#
Nice! This would probably be better named TruncateWords, no?
#