Re: {% spaceless %} abuse (?)
On Fri, Jul 6, 2012 at 4:27 AM, David Lam <david.k.lam1@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 %-)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home