2

我在 Coldfusion 2018 中使用会话变量,我试图弄清楚如何通过设置 if 语句的方式添加变量。

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

if 语句几乎相同,andor_1或者bandor_1可能并不总是存在,这就是我使用 isDefined 的原因。

我试过使用||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>

任何结合这些的帮助将cfifs不胜感激。

4

3 回答 3

3

CF 中的正确方法是“或”,而不是 ||。

但是,在您的第一个示例中,您将“或”放在 IF 语句之外。尝试这个:

 <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>
于 2018-12-05T21:19:23.023 回答
3

如果您使用的是 CF2016,则可以使用安全导航运算符,或?.( https://www.adobe.com/devnet/coldfusion/articles/language-enhancements-cf-2016.html )。你也应该开始使用 cfscript 来处理这些逻辑的事情。

<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

于 2018-12-05T23:52:53.557 回答
2

另一种选择(尽管性能不如使用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>

需要研究的是设置一些默认值,这样您就不需要运行isDefinedstructKeyExists检查。这有可能清理你的代码并使其更具可读性。

当然会有例外或需要它们的场景(例如,使用来自 API 的响应)。

于 2018-12-06T07:04:49.110 回答