Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#1067 closed enhancement (fixed)

"Spaceless" tag, removes spaces between html nodes

Reported by: brantley (deadwisdom@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This is a cool tag that I think should be added to the defaults, it removes spaces between HTML tags. This is a problem many designers has banged their heads against many walls over, where spaces between tags show up even though they are clearly not inteded to be there.
So this:

   {% spaceless %}
       <a href='#'>
            <img src='something.jpg' border='0'/>
       <a href='#'>
            <img src='something-else.jpg' border='0'/>
   {% endspaceless %}

Renders as:

<a href='#'><img src='something.jpg' border='0'/></a><a href='#'><img src='something-else.jpg' border='0'/></a>
import re

re_spaceless = re.compile(">\s+<")
def do_spaceless(parser, token):
	nodelist = parser.parse(('endspaceless',))
	return SpacelessNode(nodelist)

class SpacelessNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist
    def render(self, context):
        rendered = self.nodelist.render(context).strip()
        return re_spaceless.sub("><", rendered)

Change History (2)

comment:2 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

(In [1967]) Fixed #1067 and #276 -- Added a {% spaceless %} tag, available in all templates

Note: See TracTickets for help on using tickets.
Back to Top