Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. {% load readmore %}
  2. ...
  3.  
  4. <h4>{{ user.about|readmore:300|safe|linebreaks }}</h4>
  5.  
  6. from django import template
  7. from django.utils.html import escape
  8. from django.utils.safestring import mark_safe
  9. import re
  10.  
  11. register = template.Library()
  12.  
  13.  
  14. readmore_showscript = ''.join([
  15. "this.parentNode.style.display='none';",
  16. "this.parentNode.parentNode.getElementsByClassName('more')[0].style.display='inline';",
  17. "return false;",
  18.  
  19. ]);
  20.  
  21. @register.filter
  22. def readmore(txt, showwords=15):
  23. global readmore_showscript
  24. words = re.split(r' ', escape(txt))
  25.  
  26. if len(words) <= showwords:
  27. return txt
  28.  
  29. # wrap the more part
  30. words.insert(showwords, '<span class="more" style="display:none;">')
  31. words.append('</span>')
  32.  
  33. # insert the readmore part
  34. words.insert(showwords, '<span class="readmore">... <a href="#" onclick="')
  35. words.insert(showwords+1, readmore_showscript)
  36. words.insert(showwords+2, '">more</a>')
  37. words.insert(showwords+3, '</span>')
  38.  
  39. # Wrap with <p>
  40. words.insert(0, '<p>')
  41. words.append('</p>')
  42.  
  43. return mark_safe(' '.join(words))
  44.  
  45.  
  46. readmore.is_safe = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement