Strip whitespace from Mako template output (Pylons)

柔情痞子 提交于 2019-12-03 14:12:00

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>'

If your data are not too dynamic, you could store an optimised cache of the template output and serve this to web clients.

Similar to Dan's answer, I passed the rendered output through this function which only preserves "deliberate" whitespace. I defined that to be two carrage returns in a row (i.e an empty line)

So

Hello 
There

Becomes

Hello There

But

Hello

There

Becomes

Hello
There

Here's the code

def filter_newline(input):
    rendered_output = []
    for line in input.split("\n"):
        if line:
            # Single new-lines are removed
            rendered_output.append(line)
        else:
            # Subsequent newlines (so no body to the interveaning line) are retained
            rendered_output.append("\n")

    return "".join( rendered_output )

Execute like so (I stole part of Dan's example)

whitespace_mess = template.render(somevar="Hello \nThere")
cleaned_up_output = filter_newline(whitespace_mess)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!