Hi guys, I'm using Mako + Pylons and I've noticed a horrendous amount of whitespace in my HTML output.
How would I go about getting rid of it? Reddit manage to do it.
Hi guys, I'm using Mako + Pylons and I've noticed a horrendous amount of whitespace in my HTML output.
How would I go about getting rid of it? Reddit manage to do it.
If your data are not too dynamic, you could store an optimised cache of the template output and serve this to web clients.
I'm not sure if there's a way to do it within Mako itself but you can always just do some post-rendering processing before you serve up the page. For example, say you have the following code that generates your horrendous whitespace:
from mako import TemplateLookup
template_lookup = TemplateLookup(directories=['.'])
template = template_lookup.get_template("index.mako")
whitespace_mess = template.render(somevar="no whitespace here")
return whitespace_mess # Why stop here?
You could add in an extra step like so:
from mako import TemplateLookup
template_lookup = TemplateLookup(directories=['.'])
template = template_lookup.get_template("index.mako")
whitespace_mess = template.render(somevar="no whitespace here")
cleaned_up_output = cleanup_whitespace(whitespace_mess)
return cleaned_up_output
...where cleanup_whitespace() is some function that does what you want (it could pass it through HTML Tidy or slimmer or whatever). It isn't the most efficient way to do it but it makes for a quick example :)
There's the backslash thing.
Look at the homepage of mako http://makotemplates.org for an example.
<%def name="makerow(row)">
<tr>
% for name in row:
<td>${name}</td>\
% endfor
</tr>
</%def>
But seriously, I wouldn't spend to much time trying to format correctly the ouput. What is important is to have readable template code. I use the web inspector of Webkit (or FireBug, if you prefer) more often than "view source".
If really you want good formatted html output, you could always write a middleware that does that.
The only way to do this without post-processing would be to avoid whitespace in the template. However, that will make things very hard for you as a developer.
You need to make a decision about whether the time to clean the HTML string after the template has been rendered will save sufficient bandwidth to offset this cost. I recommend using optimized C code library to do this for you, such as lxml.html
.
>>> from lxml import html
>>> page = html.fromstring("""<html>
...
... <body>yuck, a newline! bandwidth wasted!</body>
... </html>""")
>>> html.tostring(page)
'<html><body>yuck, a newline! bandwidth wasted!</body></html>'