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)