cffile creating blank lines when storing a table

1k Views Asked by At

I've got a cfsavecontent tag that saves a table. Later I use cffile to write the saved content to a file. When I look at that file, I see that there many blank lines inserted after <td> tags in the table; and few blank lines inserted after </tr> tags. (Although it doesn't do that where the code says <tr><td>&nbsp;</td></tr> all on one line.)

Presently I have a file which contains two of those tables. The tables are generated in a loop, and the output file is created with cffile append. This file has 915 lines in it of which maybe 30 are non-blank. All my subsequent code works correctly, but this is just test data. In the real world I could have 1000 or more tables, and I am concerned about the file size.

The code:

<cfset head1 = 'from = "moxware" '>
<cfset head2 = 'to = "#hrep.PersonEmail#" '>   
<cfset head3 = 'replyto = "#replyto#" '>
<cfset head4 = 'subject = "#subject#" '>
<cfset head5 = 'type = "html" '>      

<cfsavecontent variable = "abc">
  <cfoutput>
   #head1#
   #head2#
   #head3#
   #head4#
   #head5# >
    #xyz#
  </cfoutput>
</cfsavecontent>

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "<cfmail"
    mode = "777" >

<cffile action = "append"
       file   = "/var/www/reports/moxrep/#reportout#.cfm"
       output = "#abc#"
       mode   = "777"> 

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "</cfmail>"
    mode = "777" >

Re the xyz, I am reading it in from a file:

      <cffile action = "read"
      file   = "/var/www/reports/moxrep/#reportname#.cfm"
      variable = "xyz">

and the file looks like this:

   <link rel="stylesheet" href="sample.css">
  <link rel="stylesheet" type = "text/css" href ="betty.css"/>
  <p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">Dear Customer,</span></span></p>

We were so pleased that you have signed up for one of our programs.&nbsp; Apparently you live in the city of {{1.&nbsp; Additionally we observe that your were referred to us by {{2.&nbsp; Below please find a listing of what you signed up for.</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-    family:georgia,serif">{{r</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Sincerely Yours,</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">John Jones<br />
President<br />
XYZ Corporation</span></span></p>

The file was created by a code generator, not me, so it's a bit cumbersome. Later in the code I replace everything starting with {{ ; in particular {{r gets replaced with a table, and that is where the additional space is coming from.

The append itself is not inserting any extra lines.

Does anyone know what is causing these extra blank lines in the file; and how to get rid of them?

2

There are 2 best solutions below

1
On

You may consider using cfprocessingdirective around your cfsavecontent. There is a setting in CF administrator that universally either compresses or retains unnecessary whitespace, "Enable Whitespace Management" - http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf3638e6-7ffc.html . Using the suppressWhiteSpace attribute of cfprocessingdirective, you can override this setting for a particular page or part of a page. So in your case:

<cfprocessingdirective suppressWhiteSpace="true">
<cfsavecontent variable="myvar">....
...
...
</cfsavecontent>
</cfprocessingdirective>

may help. Likewise, to ensure the retention of whitespace when building text emails, you'd use suppressWhiteSpace="false".

Cheers,

4
On

Betty, typically you need to do this carefully if you want to avoid whitespace. In particular the use of cfoutput with a query will generate lines. So this code:

<table>
<cfoutput query="myquery">
  <tr><td>#col1#</td><td>#col2#</td></tr>

</cfoutput>
</table>

will produce extra lines... but if you do this:

<cfsetting enablecfoutputonly="yes">

<cfoutput><table></cfoutput>
<cfloop query="myquery"><cfoutput><tr><td>#col1#</td><td>#col2#</td></tr></cfoutput></cfloop>
<cfoutput></table></cfoutput>

You would carefully control exactly what is allowed to be appended to the buffer. enableoutputonly does exactly what it says... it does not allow anything to "go to the buffer" unless it is enclosed in cfoutputs.

Hope this helps. As cameron says you should paste code for questions like this. That's where the answer will typically reside.

(you might also need to experiment with the "addnewline" attribute of cffile - depending on whether your problem is a line at the END of your file).


To answer your question regarding adding cfsetting... in your case you are writing CF code to a file that is then executed later (which by the way is not a great idea usually :). So in your first Append statement:

<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >

Change the "output" to be:

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "<cfsetting enablecfoutputonly=""yes""/> <cfmail"
    mode = "777" >

But Betty - you will still need to remove the line breaks from your cfsavecontent (if that's where your whitespace is coming from) because they actually ARE inside of a cfoutput. Also, your code that creates the table you are inserting might be at fault - and it is not listed here.

Finally, since this is cfmail take a look at this post regarding line breaks that may or may not have some bearing - but at least gives you one more piece of information :)

http://www.coldfusionmuse.com/index.cfm/2006/4/12/cfmail.linebreak