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
Related
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
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).
Right now in sublime text 2 when I start an if statement in Coldfusion and hit enter it will automatically indent the next line like this:
<cfif this eq that>
|
When I turn auto indent off it will leave the cursor back at the far left, which would be great, but a lot of times my code is already indented:
<cfif this eq that>
|
What I want is it to leave it where it is currently indented to, no more, no less. Like this:
<cfif this eq that>
|
Any suggestions? Thanks!
There may be other ways to make this work for you.
But, you can edit the regex string in ColdFusion.tmPreferences file under
<key>increaseIndentPattern</key>
Just add cfif and cfelse to the list
|link|meta|param|cfif|cfelse
When there is an update to the ColdFusion package though, you may have to edit again.
Edit: Make sure to update the package to the latest version. The single line tags like cfargument should not indent as expected in the updated version.
Although what you would like does not seem to be possible at the moment, see ST2 forum (maybe you posted that?)
A slightly absurd workaround that may work for you, (seems to work for me). Go to View>Syntax>Java now the auto indention should do as you please - you may lose bracket tag matching (+other things?), syntax checking may be a bit nuts (you can always flip back if necessary, try other syntax stuff), and the colour scheme will change a little, but it seems to work.
Take a look at the settings in Sublime, there is one called 'smart_indent'.
The description for this setting is:
Makes auto indent a little smarter, e.g., by indenting the next line
after an if statement in C. Requires auto_indent to be enabled.
Found some more info in the Sublime Documentation.
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"]# />