删除输出HTML代码中的空格 [英] Remove whitespace in output HTML code
问题描述
考虑具有以下内容的test.cfm文件:
< html&
< body>
< cfif foo EQ bar>
< cfset test =something/>
< / cfif>
< p>您好!< / p>
< / body>
< / html>
在浏览器中运行时,此文件输出的源代码如下: / p>
< html>
< body>
< p>您好!< / p>
< / body>
< / html>有没有办法解决这个问题?
$ b $ p <有没有办法解决这个问题?
如果您的问题是请求的大小,请使用gzip编码。
p>
如果您的问题是读取调试/ etc的源代码,请使用开发工具,例如 Firebug / etc。
然而,为提高可维护性(同时也减少空白输出),您应该做的一般事情是:
1)将任何不显示逻辑的内容移出视图。
2)转换显示逻辑
为防止输出不需要的内容,您可以可以:
-
将整个部分包含在 cfsilent http://cfdocs.org/cfsetting\"> cfsetting ,然后只在要输出的事物上使用cfoutput。
-
始终设置<$
-
当您要选择性地输出一些文本时,在CFML注释中标记非输出段<!---
... --->
用于防止自定义标签中的换行符输入)
(我从不用cfprocessingdirective打扰, 。)
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?
There's nothing to fix - the HTML is perfectly valid and functional.
If your issue is the size of request, use gzip encoding.
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) Move anything that isn't display logic out of your views.
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:
Wrap the entire section in cfsilent, to ensure nothing gets output.
Enable enablecfoutputonly
attribute of cfsetting then only use cfoutput around things you want to be output.
Always set output=false
on component and function tags.
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)
(I never bother with cfprocessingdirective, everything mentioned above solves the issues better.)
这篇关于删除输出HTML代码中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文