noemptylines

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#coding=utf-8
import re
from django.template import Node, Library
from django.utils.encoding import smart_unicode, force_unicode

register = Library()


class NoEmptyLinesNode(Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        return re.sub('\n([\ \t]*\n)+', '\n', force_unicode(
        self.nodelist.render(context).strip()))


@register.tag
def noemptylines(parser, token):
    nodelist = parser.parse(('endnoemptylines',))
    parser.delete_first_token()
    return NoEmptyLinesNode(nodelist)

Comments

akaihola (on February 25, 2008):

mikko noted about this snippet in his comment for my similar snippet

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.