• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

supressing white space using code

Enthusiast ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

We need to supress white space using code, unfortunately the server is shared and we do not have access to the administrator to make the appropriate changes.

I tried placing this at the top

<cfsetting enablecfoutputonly="true">

Then wrapping the HTML output with CFOUTPUT

However when I view the resulting source code I still get 11 line breaks above all of the code.

Is there anything else I can try?

I'm also wondering why <cfsetting enablecfoutputonly="true"> does not function

Thanks

Mark

TOPICS
Advanced techniques

Views

4.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

cfsilent

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

Thanks Dan,

I might actually have another solution. I'm creating a file with .CFM extension but it needed to display XML output, which is why I needed to remove the white space.

Looking at the documentation it looks like I needed a CFCONTENT

<cfprocessingdirective suppresswhitespace="Yes">

<cfcontent type="text/xml; charset=utf-16">

It appears to view correctly in a browser although the source code has some weird characters in it when you view source, although the white space appears to disappear now.

The only thing that gets me now is whether I need to use CFXML, I tried the example and it worked, but when I tried it myself I could not get it working right.

Any idea if CFXML a must? It looks like with CFCONTENT I have what I need

Thanks

Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

It appears that I need a combination of commands to totally elimitate the unwanted white space.

Perhaps this is because it's different types of white space? Some of it was generated when I did not have cold fusion code at the top of the page, but when I added some CF code, a bogus CFIF command, it created more space which the suppresswhite space did not seem to remove

So it looks like I had supresswhitespace removing white space that came from nowhere, the 11 blank line breaks at the top, and then the CFsilent was knocking out white space created by the bogus CFIF code.

However, I also had to add a CFCONTENT which is fine because it is an XML

<cfprocessingdirective suppresswhitespace="Yes">

<cfcontent type="text/xml; charset=utf-8">

<cfsilent>

bogus CFIF command in here to test

</cfsilent>

XML output here

</cfprocessingdirective>

I'm guessing that if I wanted to output HTML without all this white space, which would cause me to drop the CFCONTENT that it just would not work... weird.

Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

The reason for the behaviour of cfcontent lies in its reset attribute. Check out the docs

The way it works with HTML is:

etc ...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 19, 2010 Feb 19, 2010

Copy link to clipboard

Copied

So how would I ensure no white space on a CFM page that produces HTML? Avoiding those mysterious 11 blank lines at the top AND any CF parsed

code?

Thanks

Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 20, 2010 Feb 20, 2010

Copy link to clipboard

Copied

So how would I ensure no white space on a CFM page that produces HTML? Avoiding those mysterious 11 blank lines at the top AND any CF parsed

code?

I usually put a <cfcontent reset="true"> immediately before I start outputting my HTML doc.

I also wrap all my non-output-generating code in <cfsilent> tags, make sure all my CFCs and methods within them have OUTUPT="FALSE", and in custom tags make sure I control the whitespace output by using <cfsetting enablecfoutputonly="true">, and wrapping anything I need to output in cfoutput> tags (not one <cfoutput> around the whole lot: individual <cfoutput> tags around each bit I want to output, exclusing any whitespace).

I find <cfprocessingdirective suppresswhitespace="true"> to be a bit hit and miss, so don't bother so much with that.

That said... in most situations it doesn't really matter if a bit of stray indentation makes it into the resultant mark-up.  I only bother with all this carry-on if it actually really matters to the end result.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 21, 2010 Feb 21, 2010

Copy link to clipboard

Copied

This is what I ended up with, any CF above it, I'll wrap with CFSILENT. I don't have a closing </product> tag, if I do, it does not like it and throws an error, this appears to work. I had to make sure that the <?xml version did not have any line breaks above it

<cfprocessingdirective suppresswhitespace="Yes"><cfcontent type="text/xml; charset=utf-8"><?xml version="1.0" ?>

<root>

<product
primary="1"
name="name 1 in here"
guilink="http://www.etc..etc
applink="http://www.etc.etc" />

<product
primary="0"
name="name 2 in here"
guilink="http://www.etc..etc"
applink="http://www.etc,,,etc" />


</root></cfprocessingdirective>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 01, 2010 Mar 01, 2010

Copy link to clipboard

Copied

Hey there,

I had to work through this issue myself once. CFXML just wasn't working how I wanted it to. I ended up making a little tool that would convert anything to XML for me. Not sure if this is exactly what you need, but AnythingToXML might have done the hard work for you already.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 01, 2010 Mar 01, 2010

Copy link to clipboard

Copied

so far so good, by throwing enough silent tags etc at it, it appears to be behaving, although as I add more code I might end up looking at your solution

if it starts to ignore the white space commands!

Thanks

mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 20, 2010 Feb 20, 2010

Copy link to clipboard

Copied

<cfxml> is just a tool for creating in-memory XML documents: the resultant document still needs to be converted back into a string before being served to a browser or saved as a file or whatever.

XML is just text.  You can use any text-generating mechanism you like to create an XML doc.

<cfset x = '<record><name>Adam</name></record>'>

<cfoutput>#x#</cfoutput>

<cfcontent type="text/xml">

That'll serve valid XML back to the browser.  The benefit of using CF's tools to generate the XML is that they remove the chance of generating invalid XML, eg: I could very easily have missed a closing tag from my example above, which would mean my text would not be valid XML.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 08, 2010 Mar 08, 2010

Copy link to clipboard

Copied

(Shrug...)  I just have a standard "header.cfm" include which I use at the start of everything, and it includes <cfoutput reset="yes">.

Does it remove all of the white space?  No.  But it does get rid of a reasonable amount of it, and it's easy to fuhgeddaboudit.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 08, 2010 Mar 08, 2010

Copy link to clipboard

Copied

LATEST

right now I have this at the top of the CFM (XML) file

<cfprocessingdirective suppresswhitespace="Yes"><cfcontent type="text/xml; charset=utf-8"><?xml version="1.0" ?>

With no space before the <? part

Then at the end


</root></cfprocessingdirective>

It seems to give me what I need because the important part was just making sure that there was no white space at the top, otherwise the XML would not read correctly.

I don't have any CF code parsing inside the file, YET, but I will do, and from what I remember from my tests switching off cfouptut with CFSILENT around all the code seemed to work.

I'm not familiar with the CFOUTPUT RESET="yes" , what does that do? the more tags the better 😉

Thanks

Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation