Why is ColdFusion adding whitespace when I call a function in cfoutput? - coldfusion
If I do something like this in ColdFusion:
<cfoutput>foo="#foo()#"</cfoutput>
The resulting HTML has a space in front of it:
foo=" BAR"
However, if it is not a function call it works fine, i.e.:
<cfset fooOut=foo() />
<cfoutput>foo="#fooOut#"</cfoutput>
Gives this output:
foo="BAR"
Where is this extra space coming from and is there anything I can do about it?
Edit To clarify, the space is not in the value returned by my foo function:
<cffunction name="foo" access="public" returntype="string">
<cfreturn "BAR" />
</cffunction>
But I've also found that this doesn't happen with built-in functions, i.e.:
<cfoutput>"#UCase("bar")#"</cfoutput>
Prints:
"BAR"
However, it does happen if I pass the output of my function to the built-in function (this part makes no sense to me). i.e.:
<cfoutput>"#UCase(foo())#"</cfoutput>
Prints:
" BAR"
Make sure you have output attribute defined as false.
<cfcomponent output="false">
<cffunction name="foo" access="public" returntype="string" output="false">
<cfreturn "BAR">
</cffunction>
</cfcomponent>
Or, do it in cfscript style, and no extra space will be introduced.
function foo()
{
return "BAR";
}
See if this helps http://www.simonwhatley.co.uk/eliminating-whitespace-in-coldfusion
Related
a query string into a structure
I am using taffy and am passing an unknown query string to a function. I do not know the query string values passed in advance, so I am trying to use that in the function but it is not working. Please point me to right direction. Here is my code: <cffunction name="qrystringToStruct" returntype="any"> <cfargument name="myStruct" default="#structNew()#" type="struct"> <cfargument name="str" required="true" default=""> <cfscript> for(i=1; i LTE listLen(arguments.str,'&');i=i+1) { structInsert(myStruct, i, listGetAt(arguments.str,i,'&')); } </cfscript> <cfreturn myStruct> </cffunction> <cffunction name="getCourseById" taffy:verb="get" taffy:docs:hide> <cfargument name="structurl" type="any" default="" /> <cfdump var="#structurl#"> <cfdump var="#qrystringToStruct(structurl)#" abort> <cfset var local = {} /> This is how I am calling the url: http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power but all I am getting is [empty string]
Let me just preface this by saying I've never used Taffy. However with that said, I don't think it's relevant to the problem specified in your posted question. There are a few things in your code that's puzzling to me. Your call qrystringToStruct(structurl) passes one parameter but your function definition has two parameters. Why would you declare myStruct as a parameter and then <cfreturn myStruct> in your qrystringToStruct function definition? It makes no sense. You say you pass the full url to http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power? Why not just pass the querystring portion using cgi.QUERY_STRING? Anyhow, I think you're overcomplicating this and you don't need a custom function to parse out your querystring. All you need is one line of code. <cfset qryString = listToArray(cgi.QUERY_STRING, "&")> You can test it out here here.
Convert Special Characters to HTML - ColdFusion
I need to convert a lot of special characters to their html format and I am trying to do this with a function that is using ReplaceList but something is wrong with the function or the values I am passing to it. This is the function <cffunction name="HtmlUnEditFormat" access="public" returntype="string" output="no" displayname="HtmlUnEditFormat" hint="Undo escaped characters"> <cfargument name="str" type="string" required="Yes" /> <cfscript> var lEntities = "&##xE7;,&##xF4;,&##xE2;,Î,Ç,È,Ó,Ê,&OElig,Â,«,»,À,É,≤,ý,χ,∑,′,ÿ,∼,β,⌈,ñ,ß,„,´,·,–,ς,®,†,⊕,õ,η,⌉,ó,,>,φ,∠,,α,∩,↓,υ,ℑ,³,ρ,é,¹,<,¢,¸,π,⊃,÷,ƒ,¿,ê, ,∅,∀, ,γ,¡,ø,¬,à,ð,ℵ,º,ψ,⊗,δ,ö,°,≅,ª,‹,♣,â,ò,ï,♦,æ,∧,◊,è,¾,&,⊄,ν,“,∈,ç,ˆ,©,á,§,—,ë,κ,∉,⌊,≥,ì,↔,∗,ô,∞,¦,∫,¯,½,¤,≈,λ,⁄,‘,…,œ,£,♥,−,ã,ε,∇,∃,ä,μ,¼, ,≡,•,←,«,‾,∨,€,µ,≠,∪,å,ι,í,⊥,¶,→,»,û,ο,‚,ϑ,∋,∂,”,℘,‰,²,σ,⋅,š,¥,ξ,±,ℜ,þ,〉,ù,√,,∴,↑,×, ,θ,⌋,⊂,⊇,ü,’,ζ,™,î,ϖ,,〈,˜,ú,¨,∝,ϒ,ω,↵,τ,⊆,›,∏,",,♠"; var lEntitiesChars = "ç,ô,â,Î,Ç,È,Ó,Ê,Œ,Â,«,»,À,É,?,ý,?,?,?,Ÿ,?,?,?,ñ,ß,„,´,·,–,?,®,‡,?,õ,?,?,ó,,>,?,?,?,?,?,?,?,?,³,?,é,¹,<,¢,¸,?,?,÷,ƒ,¿,ê,?,?,?,?,?,¡,ø,¬,à,ð,?,º,?,?,?,ö,°,?,ª,‹,?,â,ò,ï,?,æ,?,?,è,¾,&,?,?,“,?,ç,ˆ,©,á,§,—,ë,?,?,?,?,ì,?,?,ô,?,¦,?,¯,½,¤,?,?,?,‘,…,œ,£,?,?,ã,?,?,?,ä,?,¼, ,?,•,?,«,?,?,€,µ,?,?,å,?,í,?,¶,?,»,û,?,‚,?,?,?,”,?,‰,²,?,?,š,¥,?,±,?,þ,?,ù,?,?,?,?,×,?,?,?,?,?,ü,’,?,™,î,?,?,?,˜,ú,¨,?,?,?,?,?,?,›,?,"",?,?"; </cfscript> <cfreturn ReplaceList(arguments.str, lEntities, lEntitiesChars) /> </cffunction> This is how I am calling it: <cfoutput> <cfloop query="local.q" startrow="2"> #HtmlUnEditFormat(consultServiceType)# <br /> </cfloop> </cfoutput> These are the strings I am passing to it: Security? Security Guard® Alarm System© Private Investigator; I am not getting any errors back (I had a cftry in the function before) and the strings come back the same EDIT: I've tried using #FindNoCase('©',consultServiceType)# and is returning 0 so I guess something is wrong with the string I am passing in?
You're using CF11, did you try EncodeForHTML() ?
The accepted answer is the better approach (don't reinvent the wheel), but your function isn't working because you have lEntities and lEntitiesChars mixed up. <cffunction name="HtmlUnEditFormat" access="public" returntype="string" output="no" displayname="HtmlUnEditFormat" hint="Undo escaped characters"> <cfargument name="str" type="string" required="Yes" /> <cfscript> var lEntities = "&##xE7;,&##xF4;,&##xE2;,Î,Ç,È,Ó,Ê,&OElig,Â,«,»,À,É,≤,ý,χ,∑,′,ÿ,∼,β,⌈,ñ,ß,„,´,·,–,ς,®,†,⊕,õ,η,⌉,ó,,>,φ,∠,,α,∩,↓,υ,ℑ,³,ρ,é,¹,<,¢,¸,π,⊃,÷,ƒ,¿,ê, ,∅,∀, ,γ,¡,ø,¬,à,ð,ℵ,º,ψ,⊗,δ,ö,°,≅,ª,‹,♣,â,ò,ï,♦,æ,∧,◊,è,¾,&,⊄,ν,“,∈,ç,ˆ,©,á,§,—,ë,κ,∉,⌊,≥,ì,↔,∗,ô,∞,¦,∫,¯,½,¤,≈,λ,⁄,‘,…,œ,£,♥,−,ã,ε,∇,∃,ä,μ,¼, ,≡,•,←,«,‾,∨,€,µ,≠,∪,å,ι,í,⊥,¶,→,»,û,ο,‚,ϑ,∋,∂,”,℘,‰,²,σ,⋅,š,¥,ξ,±,ℜ,þ,〉,ù,√,,∴,↑,×, ,θ,⌋,⊂,⊇,ü,’,ζ,™,î,ϖ,,〈,˜,ú,¨,∝,ϒ,ω,↵,τ,⊆,›,∏,",,♠"; var lEntitiesChars = "ç,ô,â,Î,Ç,È,Ó,Ê,Œ,Â,«,»,À,É,?,ý,?,?,?,Ÿ,?,?,?,ñ,ß,„,´,·,–,?,®,‡,?,õ,?,?,ó,,>,?,?,?,?,?,?,?,?,³,?,é,¹,<,¢,¸,?,?,÷,ƒ,¿,ê,?,?,?,?,?,¡,ø,¬,à,ð,?,º,?,?,?,ö,°,?,ª,‹,?,â,ò,ï,?,æ,?,?,è,¾,&,?,?,“,?,ç,ˆ,©,á,§,—,ë,?,?,?,?,ì,?,?,ô,?,¦,?,¯,½,¤,?,?,?,‘,…,œ,£,?,?,ã,?,?,?,ä,?,¼, ,?,•,?,«,?,?,€,µ,?,?,å,?,í,?,¶,?,»,û,?,‚,?,?,?,”,?,‰,²,?,?,š,¥,?,±,?,þ,?,ù,?,?,?,?,×,?,?,?,?,?,ü,’,?,™,î,?,?,?,˜,ú,¨,?,?,?,?,?,?,›,?,"",?,?"; </cfscript> <cfreturn ReplaceList(arguments.str, lEntitiesChars, lEntities) /> </cffunction> <cfoutput>#htmluneditformat("Company?")#</cfoutput> Further, #ReplaceList()# in both ACF and Railo/Lucee recurse through the list, which means the order of the lists matter. With the fix I suggest, ? becomes ≤. A fix to this would be to move & and the code for it to the beginning of each list. Consider this simple piece of code <cfoutput>#replacelist("abc","a,b","b,c")#</cfoutput> You would probably expect the output to be "bcc", but that's not how ReplaceList works, it works something more like this <cfset sx = "abc"> <cfset listf = "a,b"> <cfset listr = "b,c"> <cfloop from="1" to="#listlen(listf)#" index="i"> <cfset sx = replace(sx,listgetat(listf,i),listgetat(listr,i),"ALL")> <!--- iteration one replaces a with b to make bbc ---> <!--- iteration two replaces b with c to make ccc ---> </cfloop> I'm not suggesting that someone use this code when CF has the built in functionality, I'm merely explaining why it doesn't work and a pitfall of ReplaceList().
How to pass a struct to Coldfusion CFC using CFINVOKE?
I have a CFC file which handles all of the emails I'm sending form an application (using Coldfusion8). I was using CFINVOKE to call the respective function inside this CFC and passed a struct with all user data along like so: <cfscript> var User.data = {}; User.data.name = "John Doe"; User.data.email = "john#doe.com"; ... </cfscript> // call mailer <cfinvoke component="mailer_user" method="say_hi"> <cfinvokeargument name="userData" value="#User.data#"> </cfinvoke> And inside my mailer.cfc <cffunction name="say_hi" access="public" output="false"> <cfargument name="userData" type="struct" required="true" /> .... For some reason this now fails and I can only get it to work if I pass fields seperately as cfargument, which is a a pain, since I'm passing a lot of data. Question: How can I get this to work using argumentCollection. Even if I CFINVOKE like this: <cfinvoke component="mailer_user" argumentcollection="#User.data#" method="say_hi"></cfinvoke> it still doesn't do a thing. I'm setting output flags right before the cfinvoke and after, as well as inside the "say_hi" function going in and out. I'm only getting the flag before CFINVOKE. Side note: This is all done through AJAX and I'm only getting back success="false" if my CFC has an error somewhere. I only work remotely on the system, so I can't set AJAX debugging in CFADMIN
As I typed the comment above it occurred to me what the problem is likely to be. You are passing in a structure to your function. You pass User.data which has name,email,blah,etc as keys in that structure. Those keys need to match the arguments in your function <cffunction name="say_hi" access="public" output="false"> <cfargument name="name" type="struct" required="true" /> <cfargument name="email" type="struct" required="true" /> <cfargument name="blah" type="struct" required="true" /> <cfargument name="etc" type="struct" required="true" /> If you want to pass in the structure as a argument, you would need to have a user.userData as your structure of user data and your function should be <cffunction name="say_hi" access="public" output="false"> <cfargument name="userData" type="struct" required="true" /> When you pass the collection as argumentCollection you should do argumentCollection="#user#", so that the userData part matches your cfargument in the function. Clear as mud?
I think you should stay in cfscript style by writing // call mailer mailUser = createObject("component", "mailer_user"); // or new mailer_user(); for CF9+ mailUser.say_hi(User.data); That should work, if it doesn't, it's somewhere else in your code. Try looking at the error log.
You should map the variable to the data you pass, then no problem sending a struct. Do it this way <cfset objMailer = createObject("component","mailer_user") /> <cfset objMailer.say_hi(userData:user.data)/> This works even in CF7.
Ok. There was a typo inside my mailer CFC, where I had a variable with "##". As is was inside my email text it went unnoticed... So you can pass a struct allright using this: <cfinvoke component="mailer_user" method="say_hi"> <cfinvokeargument name="userData" value="#User.userdata#"> </cfinvoke> and grab it inside your called function like so: <cffunction name="say_hi" access="public" output="false" hint=""> <cfargument name="userData" type="struct" required="true" hint="user data passed" /> <cfscript> var internalInfo = ""; var User = {}; User.userdata = userData; </cfscript> ... Maybe someone else can use the snippet.
Testing for existence of FORM scope / struct in ColdFusion
Problem: When requesting the WSDL for a CFC, I get the following error: Variable FORM is undefined. It happens in this line of code, in the OnRequestStart method in application.cfc <cfif structKeyExists(form,'resetappvars')> <cfset OnApplicationStart() /> </cfif> If I request a specific method, it works fine. I have considered using cfparam to create a default form struct if none exists, but that seems like an ugly hack and I worry it will actually create the form struct in the variables or this scope of the CFC. Maybe this is a legitimate bug as well? Note: This only happens when I request the WSDL, if I invoke a method directly - the code executes as expected without problems. Update: Application.cfc code sample - just add any CFC to your app and request it with ?wsdl to see the issue. This has been tested (and failed) on ColdFusion 7 and ColdFusion 8. <cfcomponent output="false"> <cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created."> <cfset application.dsn = "my_dsn" /> <cfreturn true /> </cffunction> <cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing."> <cfargument name="TargetPage" type="string" required="true" /> <cfif structKeyExists(form,'resetappvars')> <cfset OnApplicationStart() /> </cfif> <cfreturn true /> </cffunction> </cfcomponent>
Maybe try adding a: <cfif IsDefined("form")>...</cfif> around the above code?
You could also cfparam the variable you're looking for then just change your logic a little (assuming resetAppVars is a boolean: <cfparam name="form.resetAppVars" default="false" /> ... <cfif form.resetAppVars> <cfset OnApplicationStart() /> </cfif> Edit: I'm not sure if the above code could be considered a hack, but it seems pretty standard CF, to me.
This post of Ben Nadel gives detailed list of scopes available for different types of requests. By reading it you can easily find out that form scope is not available in given context, but url is.
I've heard it's just a matter of opinion, but it seems to me that it is improper to reference your form scope within a CFC, as there is no guarantee that the form scope will be available when your cfc is invoked and when your method is called. It is better to ensure that any data that needs to be available to the method is provided explicitly to your object. This can be done either by including an argument: <cfargument name="resetAppVars" type="boolean" required="false" default="false" /> Then you check arguments.resetAppVars, and it is always defined, but defaulted to false. Or by creating an attribute on your object and creating an explicit set method: (at the top of your cfc) <cfset this.resetAppVars = false /> <cffunction name="setResetAppVars" access="public" returnType="void" output="false"> <cfargument name="flagValue" type="boolean" required="true" /> <cfset this.resetAppVars = arguments.flagValue /> </cffunction> In which case you will check against this.resetAppVars. You can also scope this locally using <cfset var resetAppVars = false /> as the declaration, which makes it a private attribute of your object, and is probably proper, so code that invokes the object cannot improperly overwrite this variable with a non-boolean type. In that case, you would simply refer directly to resetAppvars in your test, instead of using this scope.
You could also do this: <cfif NOT isSoapRequest()>... and stick your remaining logic inside that chunk.
In ColdFusion 8, can you declare a function as private using cfscript?
Normally you create a function using cfscript like: <cfscript> function foo() { return "bar"; } </cfscript> Is there a way to declare this as a private function, available only to other methods inside the same cfc? I know you can do it with tags: <cffunction name="foo" access="private"> <cfreturn "bar"> </cffunction> But I don't want to have to rewrite this large function thats already written in cfscript.
Not in ColdFusion 8. It was added in CF9, though. You don't need to rewrite the whole function, you can do this: <cffunction name="foo" returntype="string" output="false" access="private"> <cfscript> return "bar"; </cfscript> </cffunction> If you have access to CF9, the new syntax is: private string function foo() output="false" { return "bar"; }