问题描述
我使用的是Mako + Pylons,并且我注意到了HTML输出中的可怕数量的空白。
I'm using Mako + Pylons and I've noticed a horrendous amount of whitespace in my HTML output.
我该如何去除它? Reddit设法做到这一点。
How would I go about getting rid of it? Reddit manage to do it.
推荐答案
我不知道在Mako内部是否有办法做到这一点,但您可以随时只需在提供页面之前进行一些后期渲染处理即可。例如,假设你有下面的代码产生你可怕的空白:
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
...其中cleanup_whitespace()是一些你想要的功能(它可以通过或管他呢)。这不是最有效的方法,但它是一个简单的例子:)
...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 :)
这篇关于从Mako模板输出中剥去空白(Pylons)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!