- ColdFusion 9 Developer Tutorial
- John Farrar
- 362字
- 2021-08-05 16:16:36
Conditional processing with switch
You can achieve contextual selection of code segments with<cfIf>
. However, the<cfSwitch>
tag has unique style that will become a conditional processing favorite in certain scenarios. As our goal is language mastery, in this chapter we will again restructure the FAQ example using the switch
statement logic. This is by no means the best use of a switch
statement, but it will help you see how the logic works. You will not see any difference when you look at the browser from the user side.
<!--- Example: 1_22.cfm ---> <!--- Processing ---> <cfparam name="url.faq" default=""> <cfscript> faq = arrayNew(1); faq[1] = structNew(); faq[1].question = "What is the speed limit?"; faq[1].answer = "55"; faq[1].id = "a"; faq[2] = structNew(); faq[2].question = "What is a car?"; faq[2].answer = "Depends who you ask!"; faq[2].id = "b"; faq[3] = structNew(); faq[3].question = "How much is gas?"; faq[3].answer = "more than before"; faq[3].id = "c"; </cfscript> <cfswitch expression="#url.faq#"> <cfcase value="b"> <cfset question = faq[2].question> <cfset answer = faq[2].answer> </cfcase> <cfcase value="c"> <cfset question = faq[3].question> <cfset answer = faq[3].answer> </cfcase> <cfdefaultcase> <cfset question = faq[1].question> <cfset answer = faq[1].answer> </cfdefaultcase> </cfswitch> <!--- Content ---> <cfoutput> <strong>#question#</strong><br /> Answer: #answer#<br /><br /> </cfoutput> All Questions <hr /> <cfloop from="1" to="#arrayLen(faq)#" index="iFAQ"> <cfoutput> <strong>Q</strong>: <a href="?faq=#faq[iFAQ].id#"> #faq[iFAQ].question#</a><br /> </cfoutput> </cfloop>
We added an extra structure element to each array item just to make this example do a better job at illustrating the switch
condition. You will note that the hyperlink reference passed to the browser is now based on the value of the ID in each structure element. If url.faq
contains no value, then the default
value will be empty. This is because we are showing how to use the cfdefaultcase
when there is no other match. One of the beauties of this is we were able to eliminate all the protection logic we had in the previous example. In some ways, the page has been made simpler in this example.
When the page comes to the cfswitch
statement, the value of the expression is stored for comparison with each case until a match is found. If no match is found, it will check for a default case code segment. If the default case exists, then that code segment will be executed accordingly. Try it out and see if you understand what we have accomplished with this code. You might at this point even add a couple of new questions just to get a feel of what you have learned.
ColdFusion 9 added another case statement to the switch condition. This would be<cffinally>.
- RAW攝影后期從入門到精通:Photoshop+Lightroom雙修精解
- 中文版3ds Max 2013-VRay效果圖制作從新手到高手
- MATLAB計(jì)算機(jī)視覺經(jīng)典應(yīng)用
- PrestaShop 1.3 Theming – Beginner’s Guide
- Photoshop CS6 商業(yè)應(yīng)用案例實(shí)戰(zhàn)
- Adobe Photoshop 網(wǎng)頁設(shè)計(jì)與制作標(biāo)準(zhǔn)實(shí)訓(xùn)教程(CS5修訂版)
- 從零開始:AutoCAD 2015中文版機(jī)械制圖基礎(chǔ)培訓(xùn)教程
- Microsoft BizTalk Server 2010 Patterns
- 從零開始:Flash CS6中文版基礎(chǔ)培訓(xùn)教程
- 快學(xué)熟用D3
- Django 1.0 Template Development
- 新印象:CINEMA 4D電商設(shè)計(jì)基礎(chǔ)與實(shí)戰(zhàn)(全視頻微課版)
- 中文版Photoshop CS6應(yīng)用技法教程
- 科技繪圖/科研論文圖/論文配圖設(shè)計(jì)與創(chuàng)作自學(xué)手冊:科研動畫篇
- 機(jī)械CAD軟件應(yīng)用入門指導(dǎo)書