Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#1067 closed enhancement (fixed)

"Spaceless" tag, removes spaces between html nodes

Reported by: brantley (deadwisdom@… Owned by: adrian
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: UI/UX:

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)

Attachments (0)

Change History (2)

comment:2 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.