White Space / Coldfusion

孤街醉人 提交于 2020-02-01 10:58:11

问题


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?


回答1:


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>



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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



来源:https://stackoverflow.com/questions/2241910/white-space-coldfusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!