I write ColdFusion in VS code and have KamasamaK's CF extension installed. When I am inside a .cfc file and when I hit shortcut key - Ctrl-/ - it comments out code in a wrong fassion, like so:
// <cfset cfc = "wrong comment format" />
Now, I already followed instructions in this answer and have this written inside of my
C:\Users\<username>\.vscode\extensions\kamasamak.vscode-cfml-0.5.3\language-configuration.json file:
"comments": {
"lineComment": "<!--- --->",
"blockComment": [ "<!--- ", " --->" ]
}
and when I hit shortcut key inside of my .cfm file it works great:
<!--- <cfset cfm = "it works in cfm file" /> --->
but for some reason it does not work in .cfc file.
Additional information:
My .cfc file is recognized as ColdFusion file (I see CFML written in the bottom right corner of the VS Code).
When I close and reopen VS Code I see notification in the bottom right corner "Caching components". While this caching in progress commenting does work correctly and I was able to comment code out correctly like so:
<!--- // <cfset cfc = "wrong comment format" /> --->
however, after caching is done it is back to commenting out code in the wrong fashion, like so:
// <!--- // <cfset cfc = "wrong comment format" /> --->
I tested it with every single other extension disabled.
Questions:
Anybody knows what could be wrong?
Could this be a bug in the extension itself?
Edit 1.
Yesterday I left this comment:
[...] I created basic component, 3 lines of code, and formatting worked! Thank you. Need to analyze my code now.
However, this morning I tried to create the exact same basic .cfc again but this time it did not work, unlike yesterday:
<component>
// <cfset a = "a" />
</component>
Not sure what is going on, but since it works while "Caching components" pop-up is running I think the answer lies somewhere there.
Edit 2.
I just tried to create basic component for the 3rd time to test comment feature again. Immediately after I finished typing out this code
<component>
<cfset a = "a" />
</component>
I hit comment shortcut key and commenting worked:
<component>
<!--- <cfset a = "a" /> --->
</component>
However, after about 1 second later or less, I hit shortcut key again and it again commented out code wrongly:
<component>
// <!--- <cfset a = "a" /> --->
</component>
I suspect that this new file got cached and correct .cfc commenting stopped working. So, it looks like there might be a bug in extension where cached components comment code in wrong fashion.
Edit 3.
I created this issue in GitHub based on my best understanding of the problem.
Related
I have a action page that redirects to a page based on condition.
I thought of Including that page insted of redirection.
So i used cfinclude function to include this file.
But including that file doesn't work.
But when i open that page in browser it is working fine without error.
For Ex.
http://domain.com/page2.cfm?cutomerID=10
is working fine.
IN http://domain.com/page1.cfm
I am including below code.
<cfset url.customerID = 10>
<cfinclude template="page2.cfm">
Even i tried to add only HTML content which i get in page source for "http://domain.com/page2.cfm?cutomerID=10"
It is still not working including a coldfusion page with just html text without any coldfusion code.
Strange thing is When i tried to remove some text from this html content it works for some time but when i reload it stops and give the below error again.
I am not sure if cfinclude has to do anything page length or some other factors.
Screenshot for error is attached below.
Error Screenshot Image
If you are asking for a link you can have it as:
<cfoutput>
<cfset href="domain.com/page2.cfm?customerID=#customerid#&etc=#etc#" />
</cfoutput>
For your question you can use this:
<cfset custID = 10/>
<cfinclude template="page2.cfm">
In page2.cfm:
<cfif isDefined(url.customerID) || isDefined(custID)>
<!--- your etc code here --->
</cfif>
Note: If you don't want to use url.customerID you can simply delete the occurance
I am trying to create a spreadsheet (.XLS file) using Adobe ColdFusion - 8.
The code that creates a spreadsheet is:
<cfsetting enablecfoutputonly="Yes">
<cfset date_from = "#URL.date_from#">
<cfset date_to = "#URL.date_to#">
<cfset query_id="#URL.queryID#">
<cfquery name="GetEmps" datasource="cfdocexamples">
<!--- My SQL Queries Goes Here--->
</cfquery>
<cfcontent type="application/msexcel">
<cfheader name="Content-Disposition" value="filename=Employees.xls">
<cfoutput>
<table cols="4">
<cfloop query="getData">
<tr>
<td>#uid#</td>
<td>#week#</td>
<td>#book_count#</td>
</tr>
</cfloop>
</table>
</cfoutput>
Whenever I run the page, an XLS sheet is created, but I cannot find any data. The size of the created XLS file is 0.
Please Note: The Query is correct(Since when I print the output as html, I can see the table without any error/warning).
** After reading comments: UPD**:
I had updated my code and only included important code snippet now.
UPD 2:
Whenever I commented the line <cfsetting enablecfoutputonly="Yes"> , xls file is created with expected data. However, when I opened the generated file, a dialogue appears:
Please note the spreadsheet generated is perfect. Only thing that is bothering me is the above warning.
Also Note: whenever I tried to open the spreadsheet in google-docs as a preview, it says, the file could not be opened due to corrupted format.
However, I am able to open it perfectly in an MS-Excel.
Even changing content type to : <cfcontent type="application/vnd.msexcel"> , I got the same warning.
While I cannot speak exactly how to implement this, I know the developers within my organization worked around it with the Apache POI. It seemed to do the trick for them. It does have to be completed with Java through Coldfusion. Here is an example of it.
I am getting the following error on a page we are loading:
coldfusion.runtime.CfErrorWrapper
Unable to add text to HTML HEAD tag.
[empty string]
caused by
Template
Unable to add text to HTML HEAD tag.
ColdFusion was unable to add the text you specified to the output stream. This is probably because you have already used a CFFLUSH tag in your template or buffered output is turned off.
I've done a sweep of all the files that are included in our application and cannot find anything that uses CFFlush.
output is set to 'no' on all cfcs and components. I also tried adding cfsetting showdebugoutput = no in a file. That didn't help.
I turned request debugging on in cfadmin and that didn't help.
The HTML Head works fine in other parts of our app, it just seems to be on this one page.
The only thing really different about this page is that it is a particularly long page.
If it's a particularly long page, then CF may be flushing the buffer on its own. If you check in the CFAdmin, on the settings page, there is a setting for Maximum Output Buffer size. I believe the default is 1024 KB. If your page is over 1 meg of content, then CF may flush the buffer before your <cfhtmlhead /> tag runs. Try increasing the buffer size, or changing the placement of the <cfhtmlhead /> tag to see if that corrects the issue.
I've run into the same problem recently but the behavior wasn't predictable. I believe that Dan Short's answer is correct. I created some test pages to see if I could reproduce the problem. Each time TestTemplate.cfm is included, CFHTMLHEAD writes a simple JavaScript alert to the head tag. Once the buffer is reached, and the page is automatically flushed, any subsequent CFHTMLHEAD tag use will result in an error, specifically, the error in the original post. As Dan indicates, you can work your way around this issue by changing the maximum output buffer size.
file: index.cfm
<html>
<head><title>Test Page</title></head>
<body>
<cfset SampleScript = "<script src='sample.js'></script>">
cfset Count = 0>
<cfinclude template="TestTemplate.cfm">
<cfinclude template="TestTemplate.cfm">
<cfinclude template="TestTemplate.cfm">
</body>
</html>
file TestTemplate.cfm
<cfhtmlhead text="#SampleScript#">
<cfset Count++>
<cfoutput>
<h1>Count #Count#</h1>
</cfoutput>
<cfoutput>
<cfloop from="1" to="100000" index="i">
<cfscript>
j = randRange(i, 1000000);
k = randRange(i, 1000000);
l = j * k;
writeOutput(l);
</cfscript>
</cfloop>
</cfoutput>
file sample.js
alert('Boo!');
server.log showed another error that I was submitting too many fields with a POST request. I had to increase this limit on the Settings page.
To fix this, login to Coldfusion Admin, go to Memory Variables, and uncheck 'Disable updating Coldfusion internal cookies using Coldfusion tags/functions.' Save your settings and restart your website.
I have been having problems with toBase64() for awhile. I am hoping someone can tell me why CF toBase64() seems to lost something i.e. in my example it reduces the quality of an image.
I have a solution (see last code example below), but I hate not understanding why and would love to solve this is CF.
If anyone would be so kind to run the code below, you would then see that after toBase64 conversion the image quality is bad. Nothing major, but it does not look as good after the encoding. If you have never noticed, then try it, you will see what I mean.
Does anyone know why, or how to solve this in CF?
<!--- EXAMPLE 1 --->
<!--- GET IMAGE - --->
<cfset image = ImageNew("test.png")>
<!--- BEFORE GOOD--->
<cfimage action="writeToBrowser" source="#image#" >
<cfset image = toBinary(toBase64(image)) />
<!--- AFTER --->
<cfimage action="writeToBrowser" source="#image#" >
<!--- Example 2 --->
<cfset image = ImageNew("test.png")>
<cfset FileWrite(expandPath('./converted.image'),toBinary(toBase64(image))) />
<!--- without any cfimage processing, the outputted file is a JPEG --->
My solution was to use a java add-on and everything seemed ok but for reasons I won't go into here not something I can do live.
image = createObject("java","it.sauronsoftware.base64.Base64").encode(image);
toBinary(image );
Sample image output of code above can be found here: http://i56.tinypic.com/29fwiq.png
First is before toBase64 second is after, you can see the image has lost a bit of quality after toBase64 function on the second output.
Update: As pointed out by Peter, the issue seems to be with the automatic output/conversion code within the ImageObject to provide the binary output for the toBase64 function to encode.
Update I have submitted this as a bug in CF 9.0.1, please vote for bug 3177303
https://bugbase.adobe.com/index.cfm?event=bug&id=3177303
use toBase64(imageGetBlob(myImg))
see: http://blog.dkferguson.com/index.cfm/2010/4/27/All-your-base64-are-not-equal
I see no one has mentioned the imageWriteBase64() function that has been in ColdFusion since version 8. I am not sure why.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-796b.html
I have been using it this week for the first time and it seems to be going great. I have not noticed any problems with quality loss.
<cfdirectory action="list" directory="#expandPath('./images')#" name="imageDir" type="file" />
<cfloop query="imageDir">
<cfset ext = listLast(imageDir.name, ".") />
<cfset name = imageDir.name />
<cfset imagePath = imageDir.directory & "/" & name />
<cfset imageFile = imageNew(imagePath) />
<cfset imageWriteBase64(imageFile,"#expandPath('./base64')#/#name#.txt",ext, true) />
</cfloop>
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?
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>
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.
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.
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.
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.
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.
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.
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