删除输出 HTML 代码中的空格 [英] Remove whitespace in output HTML code
问题描述
考虑包含以下内容的 test.cfm 文件:
Consider test.cfm file with the following content:
<html>
<body>
<cfif foo EQ bar>
<cfset test = "something" />
</cfif>
<p>Hello!</p>
</body>
</html>
在浏览器中运行时,该文件的输出源代码如下所示:
When run in the browser, the source code of the output of this file will look like this:
<html>
<body>
<p>Hello!</p>
</body>
</html>
有没有办法解决这个问题?
Is there any way to fix this?
推荐答案
有没有办法解决这个问题?
Is there any way to fix this?
没有什么需要修复 - HTML 完全有效且功能齐全.
There's nothing to fix - the HTML is perfectly valid and functional.
如果您的问题是请求的大小,请使用 gzip 编码.
If your issue is the size of request, use gzip encoding.
如果您的问题是阅读源代码以进行调试/等,请使用开发人员工具,例如 Firebug/etc.
If your issue is reading the source for debugging/etc, use developer tools such as Firebug/etc.
但是,为了提高可维护性(同时也减少空白输出),您应该做的一般事情是:
However, general things you should be doing to improve maintainability (which at the same time also reduces whitespace output) are:
1) 将任何不属于显示逻辑的内容移出您的视图.
1) Move anything that isn't display logic out of your views.
2) 将显示逻辑适当地转换为函数和自定义标签,这两者都更容易防止/控制输出.
2) Convert display logic to functions and custom tags as appropriate, which both make it easier to prevent/control output.
为防止输出不需要的内容,您可以:
To prevent unwanted content being output, you can:
将整个部分包裹在 cfsilent 中,以确保没有任何输出.
Wrap the entire section in cfsilent, to ensure nothing gets output.
启用 cfsetting 的 enablecfoutputonly
属性,然后只使用 cfoutput 你的东西想被输出.
Enable enablecfoutputonly
attribute of cfsetting then only use cfoutput around things you want to be output.
始终在组件和功能标签上设置 output=false
.
Always set output=false
on component and function tags.
当你想有选择地输出一些文本时,将非标记非输出段包裹在 CFML 注释中 <!---
...--->
(例如,用于防止自定义标签中的换行符输出)
When you want to selectively output some text, wrap non-tag non-output segments in CFML comments <!---
...--->
(e.g. useful for preventing newline output in custom tags)
(我从不关心 cfprocessingdirective,上面提到的一切都能更好地解决问题.)
(I never bother with cfprocessingdirective, everything mentioned above solves the issues better.)
这篇关于删除输出 HTML 代码中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!