Using cfswitch

I l @ ve RuBoard

Using < cfswitch >

When you have several if statements like you had above, it is sometimes better to use <cfswitch> . You can use <cfswitch> when all your conditional logic revolves around one expression, such as the productqty variable we worked with in the last section. For example, in each of the <cfif> statements, you were comparing the product quantity to another value, and outputting a different variable based on that number. In the case of <cfswitch> , the <cfswitch> statement will look at that expression and apply one of many cases. A case is one condition for the specific expression; basically, it's your <cfif> statement without the <cfif> . For example, if the product quantity were 1, the case with the value of 1 would be executed. Using a <cfswitch> statement can come in handy when you want to include different blocks of code based on a variable. It is also a little faster to execute than multiple <cfif> statements. By dynamically including files, you can dynamically include all of your subnavigation.

NOTE

The <cfswitch> type of logic is so powerful that an entire programming methodology, FuseBox, has sprung up around it. For more information go to www.fusebox.org.


  1. Open X:\Lesson7\Start\dynamicnav.cfm from your CD.

    This file has the navigation already coded into it. This navigation is for the About page on thewirelessagent.com. You want to make this navigation dynamic based on the page that was called. That way, whenever any template is called (in this case, the About template) the subnavigation that gets included will be appropriate for the called page.

  2. Press Ctrl+N to create a new document.

    This will be where the About navigation will be created. Once this file is created, you will use <cfinclude> to incorporate this file into the dynamicnav.cfm template. Be sure to remove all the HTML tags that appear by default so you have a completely blank document.

  3. Cut the subnavigation <a href> tags out of the dynamicnav.cfm template.

    The <a href> tags start on line 88.

    By removing these tags you are completing the first step to making the subnavigation dynamic.

    NOTE

    You can turn line numbers on (or off) in ColdFusion Studio by clicking on the # symbol in the editor toolbar at the left edge of the editor window.

  4. Paste the cut navigation into the new document you just created.

    The three <a href> tags that you have pasted into the new document are all you need for the subnavigation on the About page.

  5. Save the file as aboutnav.cfm in the Lesson7/Start folder in your Inetpub/ wwwroot directory.

    By saving the file you are now ready to use the <cfswitch> tag to dynamically include the file.

  6. In the spot where you cut the navigation links, add the following:

     <cfswitch expression="#url.subnav#"> 

    This sets up the <cfswitch> tag and tells ColdFusion that the expression to use is a URL variable and that its name is subnav. You will pass a different URL variable depending on which subnavigation file you want to include.

  7. After the <cfswitch> tag, add <cfcase value="about"> .

    Each <cfcase> tag is like an if statement. In this case, if the #url.subnav# variable is equal to "about," then whatever code is enclosed in the <cfcase> tag will be exectuted.

  8. Below the <cfcase> tag, add the following:

     <cfinclude template="aboutnav.cfm"> 

    By using the <cfinclude> tag you are able to include a template file. In this case, you are including the subnavigation for the About tag.

    NOTE

    When using <cfinclude> you must specify either the full path or the relative path to the file. So either <cfinclude template="aboutnav"> or <cfinclude template="/Lesson7/start/aboutnav.cfm"> would work in this case.

  9. After the <cfinclude> tag, finish your case statement with a </cfcase> tag.

    Just like other ColdFusion tags, <cfcase> requires a corresponding closing tag.

  10. Finish up by adding the closing </cfswitch> tag.

    This completes the <cfswitch> statement and you are ready to try it out.

  11. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/dynamicnav.cfm?subnav=about .

    The subnavigation for the About template has been included. However, an error will occur if no URL variable called subnav is passed to the template. You are going to use your knowledge of <cfif> , and a new ColdFusion function called isdefined, to solve this problem.

  12. Above your <cfswitch> tag, add the following:

     <cfif isdefined("url.subnav")> 

    From earlier in the lesson, you know that the <cfif> tag looks at the expression inside itself. If the expression is true, then any code within that <cfif> tag is executed. If it's not true, then the code inside the <cfif> tag is not executed. In the code for this instance, <cfif> is using the isdefined() function to check and see if the url.subnav variable exists.

    NOTE

    Unlike most variables used in functions, ColdFusion variables must be surrounded by quotation marks when used in the isdefined function.

  13. After your </cfswitch> tag, add the closing </cfif> tag.

    You don't need a <cfelse> tag in this case, because you don't need anything to happen if the initial <cfif> statement is not true.

    Now when you access the template without a the url.subnav variable, you won't get an error.

    Before:

    After:

    What happens if there is a url.subnav variable that is passed but that is not in one of your case statements? Nothingno <cfcase> is executed. However, there may be times when you want a default case to be executed if the case doesn't match. We'll set that up now.

  14. After your final <cfcase> statement add:

     <cfdefaultcase> 

    <cfdefaultcase> is exactly what it sounds like: a default case to be executed when the one that is specified doesn't match any of the preexisting <cfcase> statements.

  15. After the <cfdefaultcase> tag, add the following:

     <cfoutput>  <font color="white" face="verdana, arial" size="1">I  don't recognize the subnav variable  #url.subnav#</font> </cfoutput> 

    This message is going to let the user know that the template didn't recognize the value of the subnav that was passed to it.

  16. Add the closing </cfdefaultcase> tag.

    Now the code for dynamically including the subnavigation is complete. The task you have completed has introduced you to one of the most powerful features of ColdFusioncode reuse. By being able to reuse the same code over and over again on multiple pages, you not only save yourself development time up front, but you also save time when you need to update your site. Let's see how it looks.

  17. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/dynamicnav.cfm?subnav=paraphernalia

    The user will see the message that says, "I don't recognize the subnav value paraphernalia." You might run into this situation if, when you made an update to your site, you forgot to remove a link to a page that you removed from your subnavigation. This way, users won't get an error but rather a custom message telling them that the value of the variable passed doesn't work.

    You will need to add <cfcase> statements for each of the main navigation iconsContact, FAQ, Features, Register, Login, and News. That way, any time a user clicks any of those links, or on pages associated with those sections of the site, the proper subnavigation will be included. Ideally, all of your top-level navigation will be conducted using an "include". That way, when you have to make changes, they will be changed automatically throughout the site. It's good practice to only ever have to edit one file to make a site-wide navigation change.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net