Preserving pretty HTML output

107 views
Skip to first unread message

Tonne

unread,
Dec 3, 2008, 2:39:12 AM12/3/08
to Django users
I am interested to see if anyone could share their solutions for
ensuring pretty HTML output.

I have found achieving it to be a very uncomfortable compromise in
that I seem to need to make my templates almost unreadable to do so,
which isn't really a practical solution.

Perhaps nicely formatted HTML markup is simply not a worthy priority
in the greater scheme of things Django. Which is a bit of a pity, as
I've always appreciated well-manicured HTML, and it has been a point
of professional pride to make my own handwritten HTML as readable as
possible.

And older thread brought up this subject too, but no conclusive
solutions were brought to light...
http://groups.google.com/group/django-users/browse_thread/thread/c9e569f7370c9c80/f11e4c24d3517233?lnk=gst&q=output+whitespace#f11e4c24d3517233

Malcolm Tredinnick

unread,
Dec 3, 2008, 4:22:01 AM12/3/08
to django...@googlegroups.com

On Tue, 2008-12-02 at 18:39 -0800, Tonne wrote:
> I am interested to see if anyone could share their solutions for
> ensuring pretty HTML output.
>
> I have found achieving it to be a very uncomfortable compromise in
> that I seem to need to make my templates almost unreadable to do so,
> which isn't really a practical solution.
>
> Perhaps nicely formatted HTML markup is simply not a worthy priority
> in the greater scheme of things Django. Which is a bit of a pity, as
> I've always appreciated well-manicured HTML, and it has been a point
> of professional pride to make my own handwritten HTML as readable as
> possible.

You're right: there's a trade-off. Almost every single piece of
machine-generated HTML makes that trade-off. You trade off output
formatting that hardly anybody will see for speed.

Every professional worth their salt strives to write neat and tidy
source. If we're writing HTML source, it will generally be neat and
tidy. In Django, we're more regularly writing template source, so that's
the bit that is edited over and over and is formatted for readability.

There's been a bunch of discussion about this and the conclusion is
this: if somebody came up with a way that wasn't eye-bleedingly ugly in
the Python source to remove any blank lines that only contained template
tags from the output, we would include it. However, it should not slow
down template rendering. A few people tried to do this on a very old
ticket and came up with 25 - 50% slow-downs, which is simply
unacceptable. I had a shot once and got something that was close to
comparable with the current speed, but it was really messy. Since then,
it's been back-burnered as there are other things to be fixed in the
template rendering space before worrying about stuff like this.

The problem is much harder than it looks on the surface, so it's not out
or orneriness that we have avoided including anything. By the time you
factor in readable templates, nested templates, included HTMl from
template tags, etc, the resulting generated HTML is only one aspect of a
multi-faceted problem.

However, if really regularly formatted HTML output is a requirement for
you and you can pay the speed penalty, you could run the results through
something like TidyLib (from the HTML tidy project) or similar before
serving it. I could imagine it wouldn't be too hard to write a
middleware that did this for all text/html (and similar) results.

Regards,
Malcolm

Tonne

unread,
Dec 3, 2008, 8:00:53 AM12/3/08
to Django users
Thank you for the detailed response, Malcolm. I wasn't aware of the
complexities of the issue and understand better now why it is the way
it is. It was something that was really bugging me, but I feel like I
can let it go now :)

I'm not skilled enough in Python to take a crack at solving the
problem myself. Although I'd prefer my HTML source output to look
good, I'll take readability of template code (and performance) over
rendered output prettiness.

Thanks again.

adelevie

unread,
Dec 3, 2008, 4:25:50 PM12/3/08
to Django users
You may also want to look at BeautifulSoup. It is an html parser
writter for python. It has a method called soup.prettify() in which
"soup" is a string of html. prettify() outputs cleanly formatted html.
Approximation:
soup = "<html><body><h1>title</h1><p>hello world</p></body></html>"
soup.pretiffy()
>>> <html>
<body>
<h1>title</h1>
<p>hello world</p>
</body>
</html>

I hope this helps.

Tonne

unread,
Dec 10, 2008, 1:21:14 AM12/10/08
to Django users
Good point, thanks very much, but I think I'll just live with the way
it currently works.

What I've done is to take a bit more care how and where I place
template tags, and this has improved things somewhat.
Reply all
Reply to author
Forward
0 new messages