diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-07-14 13:47:10 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-07-14 13:47:10 +0000 |
| commit | 3b774583711e39dae7a5cfde314288f8019f59c6 (patch) | |
| tree | f14bf8b086ad3b4d46b6cd4e1ebbfb1938836737 /django/template | |
| parent | 12b7c2a702bea68e53c438fa2c7a4a01d890695d (diff) | |
Fixed #5025 -- Add a "truncatechars" template filter. Many thanks to Chris Beaven.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16542 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/template')
| -rw-r--r-- | django/template/defaultfilters.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 74ae849314..d52f901be4 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -15,7 +15,7 @@ from django.utils.encoding import force_unicode, iri_to_uri from django.utils.html import (conditional_escape, escapejs, fix_ampersands, escape, urlize as urlize_impl, linebreaks, strip_tags) from django.utils.http import urlquote -from django.utils.text import truncate_words, truncate_html_words, wrap, phone2numeric +from django.utils.text import Truncator, wrap, phone2numeric from django.utils.safestring import mark_safe, SafeData, mark_for_escaping from django.utils.timesince import timesince, timeuntil from django.utils.translation import ugettext, ungettext @@ -244,6 +244,20 @@ def title(value): title.is_safe = True title = stringfilter(title) +def truncatechars(value, arg): + """ + Truncates a string after a certain number of characters. + + Argument: Number of characters to truncate after. + """ + try: + length = int(arg) + except ValueError: # Invalid literal for int(). + return value # Fail silently. + return Truncator(value).chars(value, length) +truncatechars.is_safe = True +truncatechars = stringfilter(truncatechars) + def truncatewords(value, arg): """ Truncates a string after a certain number of words. @@ -256,7 +270,7 @@ def truncatewords(value, arg): length = int(arg) except ValueError: # Invalid literal for int(). return value # Fail silently. - return truncate_words(value, length) + return Truncator(value).words(length, truncate=' ...') truncatewords.is_safe = True truncatewords = stringfilter(truncatewords) @@ -272,7 +286,7 @@ def truncatewords_html(value, arg): length = int(arg) except ValueError: # invalid literal for int() return value # Fail silently. - return truncate_html_words(value, length) + return Truncator(value).words(length, html=True, truncate=' ...') truncatewords_html.is_safe = True truncatewords_html = stringfilter(truncatewords_html) |
