从 Mako 模板输出中去除空格(Pylons)

发布于 2024-09-25 22:22:39 字数 79 浏览 7 评论 0 原文

我正在使用 Mako + Pylons,我注意到 HTML 输出中存在大量的空白。

我该如何摆脱它? Reddit 设法做到了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风无影 2024-10-02 22:22:39

这里有反斜杠的事情。

查看 mako http://makotemplates.org 的主页作为示例。

<%def name="makerow(row)">
    <tr>
    % for name in row:
        <td>${name}</td>\
    % endfor
    </tr>
</%def>

但说真的,我不会花太多时间尝试正确格式化输出。重要的是要有可读的模板代码。我使用 Webkit 的 Web 检查器(或者 FireBug,如果你愿意的话)比“查看源代码”更频繁。

如果你真的想要良好格式化的 html 输出,你总是可以编写一个中间件来实现这一点。

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.

甜尕妞 2024-10-02 22:22:39

无需后期处理即可实现此目的的唯一方法是避免模板中出现空格。然而,这会让作为开发人员的你变得非常困难。

您需要决定模板渲染后清理 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>'

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>'
时光病人 2024-10-02 22:22:39

我不确定 Mako 本身是否有办法做到这一点,但您始终可以在提供页面之前进行一些渲染后处理。例如,假设您有以下代码生成可怕的空白:

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?

您可以添加一个额外的步骤,如下所示:

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

...其中 cleanup_whitespace() 是一些可以执行您想要的操作的函数(它可以通过 HTML Tidy更苗条或其他)。这不是最有效的方法,但它是一个简单的例子:)

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 :)

茶底世界 2024-10-02 22:22:39

与 Dan 的答案类似,我通过此函数传递了渲染的输出,该函数仅保留“故意”的空白。我将其定义为连续两个回车符(即空行)

所以

Hello 
There

成为

Hello There

Hello

There

成为

Hello
There

这里的代码

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 )

像这样执行(我偷了丹的示例的一部分)

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

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)
╰沐子 2024-10-02 22:22:39

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文