{% spaceless %} abuse (?)

466 views
Skip to first unread message

David Lam

unread,
Jul 5, 2012, 4:27:27 PM7/5/12
to django...@googlegroups.com
hmm, kinda semi-noob, but heres my scenario

I just started working on a fairly large Django project thats been around for a couple years. 

In the templates,  I see a lot of use of {% spaceless %} tags whose apparent function is solely to trim whitespace to reduce page size/page load time or something.

That's not really what it was made for right?   When I look at the original Django ticket thing here... https://code.djangoproject.com/ticket/1067, it dosent look like it, but I'm not 100% sure I guess

Also, beyond making the template code more unreadable too, I think using it too much could impact performance right?  When I read at the django source for the spaceless tag, it eventually calls this function which does a regex replace of all whitespace in between the spaceless tags 

# <django src>/utils/html.py:87
def strip_spaces_between_tags(value):
    """Returns the given HTML with spaces between tags removed."""
    return re.sub(r'>\s+<', '><', force_unicode(value))

Russell Keith-Magee

unread,
Jul 5, 2012, 8:00:10 PM7/5/12
to django...@googlegroups.com
On Fri, Jul 6, 2012 at 4:27 AM, David Lam <david....@gmail.com> wrote:
> hmm, kinda semi-noob, but heres my scenario
>
> I just started working on a fairly large Django project thats been around
> for a couple years.
>
> In the templates, I see a lot of use of {% spaceless %} tags whose apparent
> function is solely to trim whitespace to reduce page size/page load time or
> something.
>
> That's not really what it was made for right?

Good question. I'm not really sure *what* it's supposed to be used
for. Trimming whitespace to reduce page size is one possible use; the
other is to 'humanize' the output of a block of template code that has
a lot of control structures in it (i.e., a template with lots of {% if
%} and {% for %} clauses will cause lots of blank lines and whitespace
to be injected into a template, which is a bit messy and painful to
read.

However, for me, neither of these are particularly compelling uses.

HTML code isn't supposed to be human readable, and all the modern
tools (Firebug, Safari/Chrome inspect tools) parse the markup and show
you DOM trees, not raw HTML code. And if you're worried about page
size from a performance point of view, you're going to get much better
results by turning on GZip compression in your response headers.

So why is {% spaceless %} in the template language? Well, it was added
in the early days of Django, when we were on a "accept everything in
order to build community" drive. I suspect the thinking didn't go much
further than "Yeah, I can see how that might be helpful; you've
provided a patch, so lets add it". With the benefit of hindsight, it
probably isn't as useful as we originally hoped. Now it's there, and
we'd have to go through a deprecation cycle (and probably a bunch of
painful "But I *really* need it" arguments on django-dev), so it's
easier to just live and let live.

I haven't done any testing to be sure, but it certainly wouldn't
surprise me if there's a non-trivial performance hit associated with
using spaceless. If you're performance tuning a site, and you don't
have any other reason for using spaceless (e.g., rendering a
whitespace-significant output language), I wouldn't argue against
removing uses of spaceless as a performance improvement.

Yours,
Russ Magee %-)

Micky Hulse

unread,
Jul 5, 2012, 8:29:41 PM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 5:00 PM, Russell Keith-Magee
<rus...@keith-magee.com> wrote:
> Good question. I'm not really sure *what* it's supposed to be used
> for. Trimming whitespace to reduce page size is one possible use; the

If you develop for IE6, there's the IE6 whitespace bug. One fix, that
I know of, is to remove all whitespace around the HTML.

I never use the spaceless tag myself, but just thought I would mention
another way one could utilize the tag.

David Lam

unread,
Jul 5, 2012, 8:40:44 PM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 5:00 PM, Russell Keith-Magee <rus...@keith-magee.com> wrote:
ahhhhh cool,  thanks for the insight on the history of it

if it's really the case that {% spaceless %} shouldn't be used for minifying white space on a page, I think it'd be useful to have a doc note that discourages it or something --> https://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless

something like,  "If you wanna reduce page size, its better to use web server gzip compression than to put {% spaceless %} tags everywhere.  The spaceless tag was/is intended for... "

Message has been deleted

Russell Keith-Magee

unread,
Jul 6, 2012, 9:15:40 AM7/6/12
to django...@googlegroups.com
On Fri, Jul 6, 2012 at 1:05 PM, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
> On Fri, 6 Jul 2012 08:00:10 +0800, Russell Keith-Magee
> <rus...@keith-magee.com> declaimed the following in
> gmane.comp.python.django.user:
>
>
>> HTML code isn't supposed to be human readable, and all the modern
>> tools (Firebug, Safari/Chrome inspect tools) parse the markup and show
>> you DOM trees, not raw HTML code. And if you're worried about page
>> size from a performance point of view, you're going to get much better
>> results by turning on GZip compression in your response headers.
>>
> Really? Guess that means my ancient copy of HomeSite doesn't do what
> it seems to do...

I'm sorry? I don't understand what you're driving at here. What is it
that HomeSite does that contradicts what I said?

Yours,
Russ Magee %-)

Masklinn

unread,
Jul 6, 2012, 9:38:49 AM7/6/12
to django...@googlegroups.com
An other use case is that whitespace is significant in parts of HTML: in code blocks of course but also *between inline elements*. In that case, spaceless tends to be more maintainable than removing all white space by hand end getting a single huge line.

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 6, 2012, 10:29:06 AM7/6/12
to django...@googlegroups.com
-1 on removing spaceless.

On pages where you have lots of rows in a loop, the amount of whitespace you end up with can massively increase page loading times.

Sure, you could use whitespace stripping middleware I guess, but some people prefer not to have that.

I agree that spaceless does make code look a little ugly, but without you would end up with people putting big loops on 1 single line, just to avoid the whitespace.

Cal


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Russell Keith-Magee

unread,
Jul 6, 2012, 10:36:23 AM7/6/12
to django...@googlegroups.com
On Fri, Jul 6, 2012 at 10:29 PM, Cal Leeming [Simplicity Media Ltd]
<cal.l...@simplicitymedialtd.co.uk> wrote:
> -1 on removing spaceless.
>
> On pages where you have lots of rows in a loop, the amount of whitespace you
> end up with can massively increase page loading times.

You did read my comment about using GZip compression, right? GZip is
*really* good at compressing sequences of identical characters. I'd be
deeply surprised if the effect of introducing large blocks of
whitespace into a GZip compressed HTML stream was any more than a
rounding error. It should also be *much* more effective, and involve
less computational load, than the effect of running a regex over your
page to extract whitespace (which is all spaceless is doing).

Yours,
Russ Magee %-)

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 6, 2012, 12:29:08 PM7/6/12
to django...@googlegroups.com
Oh, I should also mention that the last time I tested this was approx 6 months ago, so this may not be an issue any more.

The other issue is it can cause 'View Source' to freeze up (or in firefox, just not display anything).

On Fri, Jul 6, 2012 at 5:26 PM, Cal Leeming [Simplicity Media Ltd] <cal.l...@simplicitymedialtd.co.uk> wrote:
The last time I tested this, gzip'ing didn't really make much of a difference.

Essentially we had this block repeated 500 times:

<div>
    <div>
        something here
    </div>
        <input some really long stuff here>
    <div>
</div>

This was then also indented 4x5 places from the left, so the amount of white space was massive.

Some browsers (such as Chrome) handled it fine, but IE8/IE9 lagged pretty badly.

Feel free to test for yourself, but this was certainly our experiences from it.

Cal

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 6, 2012, 12:26:49 PM7/6/12
to django...@googlegroups.com
The last time I tested this, gzip'ing didn't really make much of a difference.

Essentially we had this block repeated 500 times:

<div>
    <div>
        something here
    </div>
        <input some really long stuff here>
    <div>
</div>

This was then also indented 4x5 places from the left, so the amount of white space was massive.

Some browsers (such as Chrome) handled it fine, but IE8/IE9 lagged pretty badly.

Feel free to test for yourself, but this was certainly our experiences from it.

Cal
Message has been deleted

David Lam

unread,
Jul 6, 2012, 4:16:34 PM7/6/12
to django...@googlegroups.com
On Fri, Jul 6, 2012 at 7:29 AM, Cal Leeming [Simplicity Media Ltd] <cal.l...@simplicitymedialtd.co.uk> wrote:
-1 on removing spaceless.

On pages where you have lots of rows in a loop, the amount of whitespace you end up with can massively increase page loading times.

Sure, you could use whitespace stripping middleware I guess, but some people prefer not to have that.

I agree that spaceless does make code look a little ugly, but without you would end up with people putting big loops on 1 single line, just to avoid the whitespace.

hm, I wanted to pick your guys' brain again

What prompted my initial email was me trying to get the server response time down.   I guess that's a different than page load time  (which feels to me like a more worthwhile thing to look at if you're trying to make your site go fast...), but I got assigned a bug to just get the 'response time' down for this one supposedly slow page etc. etc.

I'm basically just using django-debugtoolbar, looking at that CPU Time row, and seeing what I can remove or tweak from the template to bring that number down.   

One thing that I found that would bring the *CPU time* down by a bit was removing the {% spaceless %} tags littered around everywhere in the template


yeah, I wouldn't outright remove it, but maybe just change the docs to say how it should or should not be used

Russell Keith-Magee

unread,
Jul 6, 2012, 10:12:17 PM7/6/12
to django...@googlegroups.com
On Sat, Jul 7, 2012 at 1:53 AM, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
> On Fri, 6 Jul 2012 21:15:40 +0800, Russell Keith-Magee
> <rus...@keith-magee.com> declaimed the following in
> gmane.comp.python.django.user:
>
>> I'm sorry? I don't understand what you're driving at here. What is it
>> that HomeSite does that contradicts what I said?
>>
> HomeSite is (was, since Adobe killed it) an HTML editor... No
> WYSIWYG layout tools -- while it has "tag editors" (which are dialogs
> with fields for all the standard attributes of a tag), the editor itself
> was raw HTML...

Firstly, I fail to see how an editor that had it's last release (v5.5)
in 2003, and was discontinued in 2009, qualifies as a "modern tool".

Secondly, vim, Emacs, and any other text editor will also operate on
raw HTML. It's hardly a revolutionary concept. I didn't say that
humans *didn't* read or edit HTML. What I said is that *modern* tools
are trending towards using the DOM. Once upon a time, you'd use "view
source" to debug rendering problems. In that world, extra whitespace
could be annoying. These days, you open your DOM inspector, and then
go back to your template to fix any problems you find. In that
context, extra whitespace in the source is irrelevant.

Yours,
Russ Magee %-)
Reply all
Reply to author
Forward
0 new messages