空白/冷融合

发布于 2024-08-21 00:15:38 字数 102 浏览 8 评论 0原文

停止 ColdFusion 输出空白的正确方法是什么?

我知道有 cfcontent 和 cfsetting enableCFoutputOnly 。这样做的正确方法是什么?

What would be the correct way to stop the white space that ColdFusion outputs?

I know there is cfcontent and cfsetting enableCFoutputOnly. What is the correct way to do that?

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

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

发布评论

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

评论(8

So尛奶瓶 2024-08-28 00:15:38

除了 之外,。您可以使用它删除文档开头的空格。

HTML5 文档将像这样开始:

<cfcontent type="text/html; charset=utf-8" reset="true" /><!doctype html>

XML 文档:

<cfcontent reset="yes" type="text/xml; charset=utf-8" /><CFOUTPUT>#VariableHoldingXmlDocAsString#</CFOUTPUT>

这样您就不会收到 XML 文档的“序言中不允许内容”错误。

如果您从函数中获得不需要的空格,请使用 output 属性来抑制任何输出并将结果作为字符串返回 - 例如:

<cffunction name="getMyName" access="public" returntype="string" output="no">
  <cfreturn "Seybsen" />
</cffunction>

In addition to <cfsilent>, <cfsetting enablecfoutputonly="yes"> and <cfprocessingdirective suppressWhiteSpace = "true"> is <cfcontent reset="true" />. You can delete whitespaces at the beginning of your document with it.

HTML5 document would then start like this:

<cfcontent type="text/html; charset=utf-8" reset="true" /><!doctype html>

XML document:

<cfcontent reset="yes" type="text/xml; charset=utf-8" /><CFOUTPUT>#VariableHoldingXmlDocAsString#</CFOUTPUT>

This way you won't get the "Content is not allowed in prolog"-error for XML docs.

If you are getting unwanted whitespaces from a function use the output-attribute to suppress any output and return your result as string - for example:

<cffunction name="getMyName" access="public" returntype="string" output="no">
  <cfreturn "Seybsen" />
</cffunction>
冰葑 2024-08-28 00:15:38

您可以通过访问 ColdFusion 输出缓冲区来修改 ColdFusion 输出。 James Brown 最近在我们的用户组会议上演示了这一点 (James Brown) meetup.com/florida-web-developers/" rel="noreferrer">佛罗里达州中部 Web 开发人员用户组)。

<cfscript>
  out = getPageContext().getOut().getString();
  newOutput = REreplace(out, 'regex', '', 'all');
</cfscript>

执行此操作的一个好地方是在 Application.cfc onRequestEnd() 中。您的结果可能是一行 HTML,然后将其发送到浏览器。将您的 Web 服务器与 GZip 配合使用,您将大大减少带宽。

You can modify the ColdFusion output by getting access to the ColdFusion Outpout Buffer. James Brown recently demo'd this at our user group meeting (Central Florida Web Developers User Group).

<cfscript>
  out = getPageContext().getOut().getString();
  newOutput = REreplace(out, 'regex', '', 'all');
</cfscript>

A great place to do this would be in Application.cfc onRequestEnd(). Your result could be a single line of HTML which is then sent to the browser. Work with your web server to GZip and you'll cut bandwidth a great deal.

追星践月 2024-08-28 00:15:38

在标签方面,有 cfsilent

在管理员中有一个设置“启用空白管理”

进一步阅读 cfsilent 和 cfcontent 重置。

In terms of tags, there is cfsilent

In the administrator there is a setting to 'Enable whitespace management'

Futher reading on cfsilent and cfcontent reset.

笑着哭最痛 2024-08-28 00:15:38

如果 都不能满足您的要求,那么您可能过度设计了此问题。

当您纯粹出于审美原因提出问题时,我的建议是:忽略空白,它没有任何害处。

If neither <cfsilent> nor <cfsetting enablecfoutputonly="yes"> can satisfy you, then you are probably over-engineering this issue.

When you are asking solely out of aesthetic reasons, my recommendation is: Ignore the whitespace, it does not do any harm.

追我者格杀勿论 2024-08-28 00:15:38

或者,您可以确保整个页面存储在变量中,并且所有这些处理都在 cfsilent 标记内完成。例如,

<cfsilent>
    <!-- some coldfusion -->
    <cfsavecontent variable="pageContent">
        <html>
            <!-- some content -->
        </html>
    </cfsavecontent>
    <!-- reformat pageContent if required -->
</cfsilent><cfoutput>#pageContent#</cfoutput>

最后,您可以在生成页面内容之后但在输出之前执行任何其他处理,例如使用正则表达式来删除额外的空格或进行一些代码整理。

Alternatively, You can ensure your entire page is stored within a variable and all this processing is done within cfsilent tags. e.g.

<cfsilent>
    <!-- some coldfusion -->
    <cfsavecontent variable="pageContent">
        <html>
            <!-- some content -->
        </html>
    </cfsavecontent>
    <!-- reformat pageContent if required -->
</cfsilent><cfoutput>#pageContent#</cfoutput>

Finally, you can perform any additional processing after you've generated the pagecontent but before you output it e.g. a regular expression to remove additional whitespace or some code tidying.

手长情犹 2024-08-28 00:15:38

如果您使用 CFC,这里有一个提示。

如果您不希望您的方法生成任何输出,请在 中使用 output="false" (仅当您使用 CF9 脚本样式时不需要)。这将消除许多不需要的空格。

Here's a tip if you use CFC.

If you're not expecting your method to generate any output, use output="false" in <cffunction> and <cfcomponent> (not needed only if you're using CF9 script style). This will eliminate a lot of unwanted whitespaces.

_畞蕅 2024-08-28 00:15:38

如果您有权访问服务器并希望在每个页面上实现它,请搜索并安装trimflt.jar。它是一个 Java servlet 过滤器,会在发送之前删除所有空格和换行符。将 jar 放入 CF 的 /WEB-INF/lib 目录中,并编辑 web.xml 文件以添加过滤器。它也可以配置为删除注释、排除文件或扩展名以及保留特定字符串。已经运行了几年没有问题。一劳永逸的解决方案。

If you have access to the server and want to implement it on every page request search for and install trimflt.jar. It's a Java servlet filter that will remove all whitespace and line breaks before sending it off. Drop the jar in the /WEB-INF/lib dir of CF and edit the web.xml file to add the filter. Its configurable as well to remove comments, exclude files or extensions, and preserve specific strings. Been running it for a few years without a problem. A set it and forget it solution.

陌若浮生 2024-08-28 00:15:38

我发现即使使用一切可能的方法来消除空格,您的代码仍然可能有一些不需要的空格或换行符。如果您仍然遇到这种情况,您可能需要牺牲格式良好的代码来获得所需的输出。

例如,而不是:

<cfprocessingdirective suppressWhiteSpace = "true">
<cfquery ...>
 ...
 ...
 ...
</cfquery>
<cfoutput>
 Welcome to the site #query.userName#
</cfoutput>
</cfprocessingdirective>

您可能需要编码:

<cfprocessingdirective suppressWhiteSpace = "true"><cfquery ...>
 ...
 ...
 ...
</cfquery><cfoutput>Welcome to the site #query.UserName#</cfoutput></cfprocessingdirective>

这不是 CF 添加空格,而是您在格式化 CF 时添加空格。

华泰

I've found that even using every possible way to eliminate whitespace, your code may still have some unwanted spaces or line breaks. If you're still experiencing this you may need to sacrifice well formatted code for desired output.

for example, instead of:

<cfprocessingdirective suppressWhiteSpace = "true">
<cfquery ...>
 ...
 ...
 ...
</cfquery>
<cfoutput>
 Welcome to the site #query.userName#
</cfoutput>
</cfprocessingdirective>

You may need to code:

<cfprocessingdirective suppressWhiteSpace = "true"><cfquery ...>
 ...
 ...
 ...
</cfquery><cfoutput>Welcome to the site #query.UserName#</cfoutput></cfprocessingdirective>

This isn't CF adding whitespace, but you adding whitespace when formatting your CF.

HTH

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