CFSet 中的条件语句

发布于 2024-11-26 21:20:37 字数 495 浏览 0 评论 0原文

我不记得它叫什么,但我需要在 CFSet 语句内执行某种条件语句。我的意思是像下面这样

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)
};

所以我正在为邮件服务器构建一个带有 smtp 和 pop 端口的结构。我有一个从 bean 获取值的方法调用。如果该值不存在,那么它将返回一个 0 长度的字符串。如果返回值没有长度而没有 do cfif 语句,是否可以(在 ColdFusion 8 中)将值设置为 25 和 110?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笙痞 2024-12-03 21:20:38

最简洁的、在我看来最好的方法是向您的 bean 添加两个方法:getMailSmtp() 和 getMailPop()。

将条件逻辑放在那里 - 因此如果未指定,该方法将返回默认值。

像这样的事情:

<cffunction name="getMailSmtp" returntype="string" output="false">
    <cfif len(getValue("mailsmtp"))>
        <cfreturn getValue("mailsmtp") />
    <cfelse>
        <cfreturn 25 />
    </cfif>
</cffunction>

或者,您可以更改 getValue() 方法以接受第二个参数 - 默认值。然后,如果该值不存在,它将返回默认值:

resourceBean.getValue("mailsmtp", 25)

我个人会选择第一种方法,因为这意味着每当您在应用程序中调用 getMailSmtp() 时,都会应用该逻辑。

您甚至可以组合这些方法,以便您的 getMailSmtp() 方法返回 getValue("mailsmtp", 25)

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).

阪姬 2024-12-03 21:20:38
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))
};
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))
};
不必你懂 2024-12-03 21:20:38

不是 iif 的忠实粉丝。您可能会想到三元运算符。

但这

<cfset x > 3 ? true : false />

只是CF9

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文