【问题标题】:ColdFusion cfcase statements and referencing their variables?ColdFusion cfcase 语句并引用它们的变量?
【发布时间】:2014-07-14 01:33:23
【问题描述】:

如果你有这样的代码:

<cfcase value="Test">
   /**Do Stuff
</cfcase>

是否可以在 case 语句中引用该值?

我想连接一个可以处理多种情况并能够像这样动态引用变量的列表:

<cfcase value="Test,Another,Yes,No">
   <cfif this.value EQ 'Test'> blabla </cfif>
</cfcase>

我到处都找不到任何关于此的详细信息,只是好奇它是否可能。

【问题讨论】:

  • 很好的例子...包括下面的 cfscript 例子。我都投了赞成票。

标签: coldfusion coldfusion-9 coldfusion-10 coldfusion-8


【解决方案1】:

是的,您可以在 cfcase 标签中运行多个 case 语句:

<cfswitch expression="#URL.TestValue#">

    <cfcase value="Blue,Red,Orange" delimiters=",">
        <cfoutput>#URL.TestValue#</cfoutput>
    </cfcase>

    <cfcase value="Yellow">
        <cfoutput>#URL.TestValue#</cfoutput>
    </cfcase>

</cfswitch>

【讨论】:

    【解决方案2】:

    嗯...是的...如果您的&lt;cfswitch&gt; 表达式是#originalExpression#,那么导致案例触发的值将是...#originalExpression#。没有必要为此感到棘手!

    IE:你需要做这样的事情:

    <cfswitch expression="#originalExpression#">
        <cfcase value="Test,Another,Yes,No">
            <!--- stuff common to all of Test,Another,Yes,No ---->
    
            <!--- stuff specific to various cases --->
            <cfif originalExpression EQ "test">
                <!--- do stuff ---->
            <cfelseif listFindNoCase("Yes,No", originalExpression)>
                <!--- do stuff ---->
            <cfelse>
                <!--- do stuff for "another" --->
            </cfif>
        </cfcase>
        <!--- other cases etc --->
    </cfswitch>
    

    【讨论】:

      【解决方案3】:

      我认为使用 ColdFusion 标签是不可能的。你可以用&lt;cfscript&gt;做类似的事情

      switch (expression)    {
      
          case "Test" :
             // Do some extra stuff
             // No break here
      
          case "Another" : case "Yes" : case "No" :
            // Do yet some normal stuff
            break;
          }
      

      免责声明:我将想要维护此代码

      【讨论】:

        猜你喜欢
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 2020-01-24
        • 2022-01-04
        • 1970-01-01
        相关资源
        最近更新 更多