Reputation: 9759
I can't remember what it is called, but I need to do a sort of conditional statement inside of a CFSet statement. What I mean is something like the following
siteSettings = {
mailserversmtpport = resourceBean.getValue('mailsmtp'), // SMTP Port (If the method returns no len() then default to 25)
mailserverpopport = resourceBean.getValue('mailpop'), // POP port (If the method returns no len() then default to 110)
};
So I am building a structure with the smtp and pop port for a mail server. I have a method call that gets a value from a bean. If that value doesn't exist then it is just going to return a 0 length string. Is it possible (in ColdFusion 8) to have the value be 25 and 110 if returned values have no length without do cfif statements?
Upvotes: 2
Views: 1225
Reputation: 561
The neatest, and in my opinion best, way to do this would be to add two methods to your bean: getMailSmtp() and getMailPop().
Put the conditional logic in there - so the method returns your default value if it's not specified.
Something like this:
<cffunction name="getMailSmtp" returntype="string" output="false">
<cfif len(getValue("mailsmtp"))>
<cfreturn getValue("mailsmtp") />
<cfelse>
<cfreturn 25 />
</cfif>
</cffunction>
Alternatively, you could alter your getValue() method to accept a second argument - a default value. Then, if the value doesn't exist, it would return the default:
resourceBean.getValue("mailsmtp", 25)
I'd personally go for the first method, as this means that any time you call getMailSmtp() in your application, the logic is applied.
You could even combine the methods, so your getMailSmtp() method returns getValue("mailsmtp", 25)
.
Upvotes: 3
Reputation: 4758
Not a big fan of iif. Your probably thinking of the ternary operator.
which is
<cfset x > 3 ? true : false />
But it's CF9 only
Upvotes: 0
Reputation: 3762
siteSettings = {
mailserversmtpport = iif(len(resourceBean.getValue('mailsmtp')),de(resourceBean.getValue('mailsmtp')),de(25)),
mailserverpopport = iif(len(resourceBean.getValue('mailpop')),de(resourceBean.getValue('mailpop')),de(110))
};
Upvotes: 3