Just whipping it seems to do what you want and there is no dependency on any external JS libraries.
DISCLAIMER: I have not tried this in IE, but chrome and firefox work fine.
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
register = template.Library()
import re
readmore_showscript = ''.join([
"this.parentNode.style.display='none';",
"this.parentNode.parentNode.getElementsByClassName('more')[0].style.display='inline';",
"return false;",
]);
@register.filter
def readmore(txt, showwords=15):
global readmore_showscript
words = re.split(r' ', escape(txt))
if len(words) <= showwords:
return txt
words.insert(showwords, '<span class="more" style="display:none;">')
words.append('</span>')
words.insert(showwords, '<span class="readmore">... <a href="#" onclick="')
words.insert(showwords+1, readmore_showscript)
words.insert(showwords+2, '">read more</a>')
words.insert(showwords+3, '</span>')
words.insert(0, '<p>')
words.append('</p>')
return mark_safe(' '.join(words))
readmore.is_safe = True
To use it, just create the templatetags folder in your application, create a file there __init__.py, and then release this code in readmore.py.
, , : {% load readmore %}
:
{{ some_long_text_var|readmore:15 }}
: 15 , .
, - ajax, .