简体   繁体   English

从Mako模板输出中去除空白(定向塔)

[英]Strip whitespace from Mako template output (Pylons)

I'm using Mako + Pylons and I've noticed a horrendous amount of whitespace in my HTML output. 我使用的是Mako + Pylons,我注意到HTML输出中有大量空白。

How would I go about getting rid of it? 我将如何摆脱它? Reddit manage to do it. Reddit设法做到了。

There's the backslash thing. 有反斜杠的东西。

Look at the homepage of mako http://makotemplates.org for an example. 请参阅mako的主页http://makotemplates.org作为示例。

<%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". 我比“查看源代码”更频繁地使用Webkit(或FireBug,如果您愿意)的Web检查器。

If really you want good formatted html output, you could always write a middleware that does that. 如果确实要获得格式良好的html输出,则可以始终编写实现此目的的中间件。

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. 您需要确定在呈现模板后清理HTML字符串的时间是否会节省足够的带宽以抵消此成本。 I recommend using optimized C code library to do this for you, such as lxml.html . 我建议使用优化的C代码库为您做到这一点,例如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>'

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. 我不确定Mako本身是否有办法做到这一点,但是您始终可以在提供页面之前进行一些后期渲染处理。 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). ...其中cleanup_whitespace()是执行您想要的功能的函数(可以通过HTML Tidyslimer或其他方法传递它)。 It isn't the most efficient way to do it but it makes for a quick example :) 这不是最有效的方法,但是可以举一个简单的例子:)

Similar to Dan's answer, I passed the rendered output through this function which only preserves "deliberate" whitespace. 与Dan的答案类似,我通过此函数传递了呈现的输出,该函数仅保留“故意的”空白。 I defined that to be two carrage returns in a row (ie 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) 这样执行(我偷了Dan的示例的一部分)

whitespace_mess = template.render(somevar="Hello \nThere")
cleaned_up_output = filter_newline(whitespace_mess)

如果数据不是太动态,则可以存储模板输出的优化缓存并将其提供给Web客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM