smartspaceless w Django
Wbudowany w Django tag spaceless jest dziwny, zjada wszystkie spacje między tagami i żadnych w środku. W większości zastosowań to wystarcza ale...
Permalink
Django smartspaceless
Default django spaceless tag is weird, it consumes all spaces between tags and none inside of them. It's good for small parts of site but...
...But I like my html to be cleared of all unnecessary mess which in many cases end up like this:
<a href="...">link1</a> <a href="...">link2</a>generates awful:
link1link2
In addition if you like having your templates indented it leaves awful things like:
<div> Test text testt </div>
Here's my "smart" version of strip_spaces_between_tags which can be used with overrided spaceless template tag:
def strip_multiple_spaces_between_tags(value): """Returns the given HTML with multiple spaces between tags removed.""" def tagcheck(obj, prefix='', sufix=''): tag = obj.group(1).lower() if tag[0] == '/': tag = tag[1:] if tag != 'pre': return '%s<%s>%s' % (prefix, ''.join(obj.groups('')), sufix) return obj.group(0) def tagprecheck(obj): return tagcheck(obj, sufix=' ') def tagpostcheck(obj): return tagcheck(obj, prefix=' ') value = force_unicode(value) value = re.sub(r'<(/?[\w]+)([^>]*)>\s{2,}', tagprecheck, value); value = re.sub(r'\s{2,}<(/?[\w]+)([^>]*)>', tagpostcheck, value) value = re.sub(r'>\s{2,}<', '> <', value) return value strip_multiple_spaces_between_tags = allow_lazy(strip_multiple_spaces_between_tags, unicode)
This does few things:
- it removes all multiple spaces between tags,
- it removes all multiple spaces at from "<tag> HERE text HERE </tag>" on all tags except pre.
Archives
- February 2016
- October 2015
- September 2015
- July 2013
- June 2013
- April 2013
- March 2013
- December 2011
- November 2011
- July 2011
- June 2011
- February 2010
- September 2009
- March 2009
- February 2009