1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class SpacelessNode(template.Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
return strip_spaces_between_tags(self.nodelist.render(context).strip())
@register.tag
def spaceless(parser, token):
"""like spaceless, just removes the spaces at the beginning of a line too"""
nodelist = parser.parse(('endspaceless',))
parser.delete_first_token()
return SpacelessNode(nodelist)
def strip_spaces_between_tags(value):
"Return the given HTML with spaces between tags removed."
value = re.sub(r'\n\s+', '\n', force_unicode(value)) # Replace all leading spaces at the beginning of a line!
return re.sub(r'>\s+<', '><', force_unicode(value))
strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode)
|
Comments
Note that this will erase all leading spaces in pre-Tags too.
#