Line & paragraph chopping

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.template import Library

register = Library()

@register.filter
def paragraphs(var, arg):
    paras = var.replace("\r\n", "\n").split("\n\n")
    return "\n\n".join(paras[:int(arg)])

@register.filter
def lines(var, arg):
    lines = var.replace("\r\n", "\n").split("\n")
    return "\n".join(lines[:int(arg)])

More like this

  1. Custom Admin Redirects by leitjohn 2 years, 10 months ago
  2. JSONField by deadwisdom 4 years, 5 months ago
  3. Generic object_detail view with multiple named URL filters by cotton 1 month, 3 weeks ago
  4. Format transition middleware by limodou 4 years, 11 months ago
  5. ByteSplitterField by Lacour 5 months, 2 weeks ago

Comments

(Forgotten your password?)