如何使用 Mako 模板去除空格和换行符?我的 12362 行 HTML 文件正在杀死 IE
我在 Pylons 网站中使用 Mako 模板系统,但在剥离空格时遇到一些问题。
我去除空格的理由是生成的 HTML 文件有 12363 行代码。我认为这就是 Internet Explorer 在尝试加载时挂起的原因。
我希望能够让我的 HTML 文件看起来漂亮、整洁,这样我就可以轻松地对其进行更改,并使生成的输出看起来丑陋和混乱,以减少文件大小和内存使用量。
Mako 文档 http://www.makotemplates.org/docs/filtering.html说你可以使用 trim
标志,但这似乎不起作用。示例代码:
<div id="content">
${next.body() | trim}
</div>
我能够去除换行符的唯一方法是在每行末尾添加 \
(反斜杠)。在编码视图时这相当烦人,我更喜欢有一个集中的解决方案。
如何删除空格/换行符?
I'm using the Mako template system in my Pylons website and am having some issues with stripping whitespace.
My reasoning for stripping whitespace is the generated HTML file comes out as 12363 lines of code. This, I assume, is why Internet Explorer is hanging when it tries to load it.
I want to be able to have my HTML file look nice and neat so I can make changes to it with ease and have the generated output look as ugly and messy as required to cut down on filesize and memory usage.
The Mako documentation http://www.makotemplates.org/docs/filtering.html says you can use the trim
flag but that doesn't seem to work. Example code:
<div id="content">
${next.body() | trim}
</div>
The only way I've been able to strip the newlines is to add a \
(backslash) to the end of each line. This is rather annoying when coding the views and I'd prefer to have a centralized solution.
How do I remove the whitespace/newlines ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为 IE 挂起是由于文件大小造成的。您可能刚刚发现了一个标记组合,该组合会遇到 IE 错误,导致其冻结。尝试修剪文档,直到不再发生冻结,以隔离有问题的标记模式(然后避免使用标记模式),或者尝试更改标记,直到不再发生这种情况。
另一件好事是通过 HTML 验证器运行您的页面并修复报告的任何问题。
I don't believe IE hanging is due to the file size. You have probably just found a combination of markup that hits an IE bug that causes it to freeze. Try trimming down you document until the freeze no longer happens to isolate the offending piece of markup pattern (and then avoid the markup pattern), or try changing the markup until this no longer happens.
Another good thing to do would be to run your page through HTML validator and fixing any issues reported.
我猜测修剪过滤器将您的 html 视为单个字符串,并且仅去除前导和尾随空白字符。您希望它从每一行中去除空格。我将创建一个过滤器并迭代每一行。
I'm guessing that the trim filter is treating your html as a single string, and only stripping the leading and trailing whitespace characters. You want it to strip whitespace from every line. I would create a filter and iterate over each line.
您可以创建自己的简单 WSGI 中间件,以在渲染模板后去除模板中的空白(可能使用 这个答案)。
下面是一个可以帮助您入门的简单示例。 注意:我编写了这段代码,但没有测试,甚至没有运行它;它可能第一次工作,但它可能无法满足您的需求,因此您需要自己对此进行扩展。
You could create your own simple WSGI Middleware to strip whitespace from your templates after they've been rendered (possibly using the methods described in this answer).
Below is a quick example which might help you get started. Note: I wrote this code without testing or even running it; it may work first time, but it probably won't suit your needs so you'll need to expand on this yourself.