3

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?

Betty Mock
  • 1,373
  • 11
  • 23
  • We can't really comment sensibly here without seeing your code. It will not be CF adding the whitespace, it'll be your code. – Adam Cameron Aug 17 '13 at 20:35
  • You're still not including the relevant code. You say this: "in particular {{r gets replaced with a table, and that is where the additional space is coming from.", but you don't include the code that does this. – Adam Cameron Aug 18 '13 at 13:38
  • 1
    You will be capturing the indentation in the CFSAVECONTENT block. If you don't want that, use string concatentation instead of CFSAVECONTENT. – Adam Cameron Aug 18 '13 at 13:39
  • Are you sure you don't have a problem with ADDNEWLINE? I would add it to each of these append operations just to be safe (as in cffile addnewline="NO") ... the default behavior is YES for append. – Mark A Kruger Aug 18 '13 at 17:09
  • Betty... ok you say you replace {{r with a table and that's where the space is coming from. but where is the code that creates the table? I'm not sure I see that here do I? – Mark A Kruger Aug 18 '13 at 17:13
  • Do you have whitespace management turned on in ColdFusion Administrator? – Scott Stroz Aug 19 '13 at 12:28
  • Scott -- turning on the whitespace management in the Administrator worked. Yay! Now I know. Mark, I didn't send the code for producing the table because it is over 1000 lines of mayhem. Even if I could get it to you, would you want to read it? (I intend to review it and hopefully cut it down, but I think a 20% cut is the best I could do). Everyone -- thank you for helping. I'd never get this project done without Stack Overflow. – Betty Mock Aug 20 '13 at 00:21

2 Answers2

0

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

Mark A Kruger
  • 7,183
  • 20
  • 21
  • I've pasted in some code; sorry I can't direct you to a website but I'm running on localhost right now. I am not filling the file from a query. I do not have a problem at the end of the append. As you can see, the stuff in the cfcontent tab is enclosed in cfoutputs. I'll see if the enablecfoutputonly works, and report back. thanks. – Betty Mock Aug 18 '13 at 05:01
  • It's simpler than that. When you use cfsavecontent, carriage returns in your source code get included in the variable. That's what the samples in this answer are all about. – Dan Bracuk Aug 18 '13 at 18:55
  • It tried it with a test file and it worked fine. I tried it with the real program and it did not work. What Dan says about cfsavecontent seems to be what's happening. But I don't know how to get rid of the The example you gave does not show how to get that output into a tag in there. Can you help me out here by explaining a little further? – Betty Mock Aug 19 '13 at 00:24
  • cfsavecontent is fine. However, if you don't want carriage returns in your source codes to be included in the coldfusion variable, don't put carriage returns in your source code. Mark's second code sample demonstrates this. – Dan Bracuk Aug 19 '13 at 00:35
0

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,

GumbyG
  • 488
  • 4
  • 8
  • I eventually suppressed the white space using the cold fusion administrator. It was not perfect, but solved the problem of ridiculous numbers of extra lines. I will try out the cfprocessingdirective and see what that does for me. – Betty Mock Dec 12 '14 at 03:44