为了避免嵌套if语句并提高可读性,我想在Coldfusion中创建一个 switch(true){ ... }
语句。我在PHP中经常使用这个,但是当我尝试这个ColdFusion中,我得到以下错误在初始化:在coldfusion中切换(true)动态情况?
模板错误
这种表达必须有一个恒定值。
这种情况发生在开关的情况下在其条件使用一个变量,如:
//this example throws the error
switch(true){
case foo == 1:
writeOutput('foo is 1');
break;
}
使用switch(真){...}语句常数值(如错误解释)不工作:
//this example doesn't throw the error
switch(true){
case 1 == 1:
writeOutput('1 is 1');
break;
}
有没有什么办法让Coldfusion的第一个声明工作?也许是对变量或一些技巧的评估,或者这在Coldfusion中是不可行的?
不是。除非它们改变了CF11中的行为,否则它必须是一个常数或[写成符合常数规则的方式](http://stackoverflow.com/questions/31910902/coldfusion-cfscript-switch-case-其中的病例之间-A-范围)。 – Leigh