really spaceless (trim spaces at line start)

 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)

More like this

  1. Smart Spaceless by btaylordesign 3 years ago
  2. Plaintext format (advanced spaceless) by hoverhell 3 years ago
  3. smart spaceless by nedbatchelder 6 years, 1 month ago
  4. Updated version of StripWhitespaceMiddleware (v1.1) by sleepycal 2 years, 10 months ago
  5. Skip only specified spaces by axil 2 years, 2 months ago

Comments

bartTC (on January 16, 2008):

Note that this will erase all leading spaces in pre-Tags too.

#

gregb (on September 3, 2009):

You need to add in some import statements at the top there...

#

(Forgotten your password?)