Took over some code and I found this logic:
<cfif FALSE>
execute this code
<cfelse>
execute this
</cfif>
There is no conditional or expresssion for the IF statement. Just hanging out there as 'FALSE'. What is this statement evaluating then? OR What is the default expression of a cfif statement?
That example will always run the "execute this" bit (the cfelse). The "execute this code" bit will never run. Looks like an odd way of preventing a chunk of old code from running. The "execute this code" bit could've just been commented-out or removed (and thus no need for the cfif/cfelse tags).
Related
I am having trouble using the "OR" operators in CF. I have tried writing many different or statements and none seem to work they are stopping at the first 'eq' to:
<cfif form.chosen eq 'Vickih' || 'AlyssaH'>
<cfif form.chosen eq 'Vickih' or 'AlyssaH'>
Am I just mistyping or misusing the "OR" statement? I need to make a list of like 8 names. I am trying to prevent the "else if" repeating the same code over and over.
Any help is greatly appreciated. The docs I am looking up say this should work. But the code is not running with the second name only the first name.
The correct syntax is
<cfif form.chosen eq 'Vickih' OR form.chosen eq 'AlyssaH'>
you could use the listFind or listFindNoCase function and do something like this:
<cfif listFindNoCase('Vickih,AlyssaH',form.chosen)>
Adobe documentation
I recently had the need to match against two strings in ColdFusion and ran into this scenario during my loop:
<cfif "0" IS NOT "NO">
Generally during the loop it looks something like this:
<cfif "AM" IS NOT "BA">
Now both of these values were variables (I wasn't just typing it out for fun) and I was using "0" as a default value for the first variable to match against (since the second variables would never be 0) but both of these values changed in the loop I was running. I easily fixed this by setting my default value to -- instead of 0 but I tried researching and found nothing indicating there was a way to get around the falsey nature of strings when evaluating them.
Is there no Operator or trick to match on the strings themselves and ignore their truthyness or falseyness in ColdFusion?
The compare function will help you. This:
writedump(compare("0", "NO"));
returns -1.
This page will tell you what that means.
On ACF 10, I have a function with the following signature...
<cffunction name="...">
<cfargument ...>
<cfdump var="#arguments#" output="#expandPath('/files/logs/debug.txt')#">
<cfscript>
writeDump(var="#arguments#", output="#expandPath('/files/logs/debug.txt')#");
</cfscript>
</cffunction>
The first tag-based dump works, but the second script-based dump does not. I've had this problem before and I've definitely gotten the tag based dump to work before. It seems random. Sometimes it works, sometimes it doesn't. Once it starts working, it continues... but when it doesn't I can't figure out why.
On the script based variant, I've also tried...
WriteDump(...)
removing the comma separating the two attributes (an other working example is written this way oddly enough)
removing the trailing semi-colon which apparently isn't always necessary per this... Why writedump function doesn't require semicolon in cfscript?
Anyone else having this problem and or know what the solution could be?
PS. Anywhere there's a "..." above, it's just to note that that bit is unimportant
I'm reviewing some code inside of a cfmodule it got me scratching my head.
The cfmodule is being called like:
<cfmodule template="/cfmods/mod1.cfm" mode="breadcrumbs">
and the code inside has a series of cfelse statements but this one is the one that gave me pause.
<cfelseif isdefined("attributes.mode")
AND NOT comparenocase("breadcrumbs", attributes.mode)>
Can someone translate this into spoken words? I know CompareNoCase will return a negative number, 0, or a positive number as a result. So what does adding the the word not do, check for the opposite of what was returned from CompareNoCase?
AND (CONDITION) checks if the condition is TRUE.
AND NOT (CONDITION) checks if the condition is FALSE.
So that statement is saying:
If Attributes.Mode exists as a variable and attributes.mode IS EQUAL to Breadcrumbs regardless of case, THEN...
I am getting an an error "Invalid CFML construct found"
iif(stImages[id][1]["seolink"] is not "", stImages[id][1]["seolink"], stImages[id][1]["url"]) />
what i am doing here wrong?
Try:
iif(stImages[id][1]["seolink"] is not "", DE(stImages[id][1]["seolink"]), DE(stImages[id][1]["url"])) />
For those readers playing from home (as it were), IIF can be an unruly beast because of the double evaluation it does. So
#IIF(myVal EQ "", "thisThing", "thatThing")#
LOOKS like it will simply return the first or second strings, but in fact it will return the content of the VARIABLES "thisThing" or "thatThing" (or throw an error that they don't exist). So say it with me: "IIF() and DE() GO TOGETHER LIKE MUTUALLY BENEFICIAL PARASITIC LIFEFORMS". "DE" as in "Delayed Evaluation". So if you want the above statement to return the first or second string, you need:
#IIF(myVal EQ "", DE("thisThing"), DE("thatThing"))#
Now, you can certainly use this feature to evaluate a field twice and NOT use "DE()", but that means you're using some kind of dynamic variable name, and it could be argued that doing that isn't best practice. Not that I haven't done that exact thing, but it should be copiously commented, because if you don't the person who maintains the code after you will probably want to kill you.
By the way, there's no mystery to "DE()". These two statements are equivalent:
#DE("thisThing")#
#"""thisThing"""#
See what's going on? "DE()" simply puts double quotes around something. So one set of quotes gets "stripped off" the first time it gets evaluated, and then the quoted string gets returned from the function. Clear as mud?
See why people don't like IIF? It's very handy in certain situations, but is a contextual mess and contributes to code that makes people go "HWUUUH??" So that's why people say to avoid it if possible.
I would avoid iif where you can,
iif(stImages[id][1]["seolink"] is not "", DE(stImages[id][1]["seolink"]), DE(stImages[id][1]["url"])) />
<cfif stImages[id][1]["seolink"] is not "">#stImages[id][1]["seolink"]#<cfelse>#stImages[id][1]["url"]#</cfif>
or if you have ColdFusion 9
<cfset stImages[id][1]["seolink"] is not "" ? #stImages[id][1]["seolink"]# : #stImages[id][1]["url"]# />