Opened 19 years ago

Closed 19 years ago

Last modified 18 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

Description

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>
       <a href='#'>
            <img src='something-else.jpg' border='0'/>
       </a>
   {% 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',))
	parser.delete_first_token()
	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 by Adrian Holovaty, 19 years ago

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