Reputation: 7397
I am using session variables in Coldfusion 2018 and I am trying to figure out how to add a variable with the way my if statements are set up.
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1') >
<cfif session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
or
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1') >
<cfif session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
The if statements are almost identical andor_1
or bandor_1
but either one might not always exist which is why I am using the isDefined.
I have tried using ||
and or
.
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1')
|| isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1')>
<cfif session.checkout.info.andor_1 eq "And" || session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
Any help combining these cfifs
would be greatly appreciated.
Upvotes: 2
Views: 1527
Reputation: 424
Another option (although less performant than using structKeyExists()
.
<cfif isDefined("session.checkout.info.andor_1") AND session.checkout.info.andor_1 eq "And"
OR isDefined("session.checkout.info.bandor_1") AND session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
Something to look into would be setting some default values so that you don't need to run isDefined
or structKeyExists
checks. That has the potential to clean up your code quite a bit and make it more readable.
There are of course going to be exceptions or scenarios where they are necessary (e.g. consuming a response from an API).
Upvotes: 2
Reputation: 4786
If you are using CF2016, you can use the Safe Navigation Operator, or ?.
(https://www.adobe.com/devnet/coldfusion/articles/language-enhancements-cf-2016.html). And you should also start using cfscript for these kinds of logic things.
<cfscript>
// Setup my struct. Session is a struct. I renamed it for my example since it's a special word.
s = {
checkout : {
info : {
andor_1 : "And" ,
bndor_1 : "And"
}
}
} ;
//writeDump(session);
// If CF2016+ Safe Navigation Operator To The Rescue!
if( s?.checkout?.info?.andor_1 == "And" || s?.checkout?.info?.bndor_1 == "And" ) {
writeOutput("<strong>- All signatures are required.</strong>");
}
</cfscript>
https://trycf.com/gist/a82b8466c427fb40b53bbc506e4d419d/lucee5?theme=monokai
Upvotes: 3
Reputation: 754
The correct way in CF is 'OR' as opposed to ||.
However, in your first example you've place the "OR" outside of your IF statements. Try this:
<cfif isDefined("session") AND structKeyExists(session, 'checkout') AND structKeyExists(session.checkout, 'info')
AND (
(structKeyExists(session.checkout.info, 'andor_1') AND session.checkout.info.andor_1 eq "And")
OR
(structKeyExists(session.checkout.info, 'bandor_1') AND session.checkout.info.bandor_1 eq "And")
)>
<strong>- All signatures are required.</strong>
</cfif>
Upvotes: 3