Passing a Variable Between Two Templates

I l @ ve RuBoard

Because the Web is stateless (that is, a connection is not maintained between the Web server and the browser), it is sometimes necessary to pass a variable to another page in order to be able to keep using it. If you needed to pass an ID for a product from one window to the next , you could store it as a shared memory variable such as a session variable, but it's much easier to pass this variable to the template instead.

The easiest way to do this is through the URL of the template to which we want to pass the variable. When a variable is passed through the URL, it is appended to the end of the URL. If you have spent any time surfing the Web you have undoubtedly seen a URL variable. Many times they look like a mess of gobbledygook at the end of a recognizable URL. But that's not always the case; many times, simple values are passed through the URL. For example, the following URL passes several values to the next template:

http://www.ebags.com/index.cfm?sub_site_ID=18&Tab_20_Link=Top18

Either waywhether the value is complex and makes no sense or the value is reasonably simple like the one abovethe value is accessible to the template it was passed to. One instance of when you might use this process would be if a customer wanted to change how a date was formatted. Say that they wanted the date formatted in the European style, with the day first. You could let them choose the format they like and then pass that format from template to template as they traverse your site. This is certainly not the best way to accomplish this, but it will illustrate to you how variables are passed.

  1. In date.cfm on line 107, create the following links to the dateformat.cfm template:

     <a href="dateformat.cfm">dd/mm/yy</a> ex: 15/10/01  <a href="dateformat.cfm">mmmm dd, yyyy</a> ex: October 15, 2001 Format 1: <a href="dateformat.cfm">dd/mm/yy</a> (ex: 15/10/01) <br> Format 2: <a href="dateformat.cfm">mmmm dd, yyyy</a> (ex: October 15, 2001)<br> 

    This creates simple HTML links to the ColdFusion template called dateformat.cfm. In order to pass a variable to that template, we are going to append that variable to the end of the URL.

  2. Modify the links by adding the following to the end:

     ?format=#URLEncodedFormat(dd/mm/yy)#, and ?format=#URLEncodedFormat(mmmm dd, yyyy)#  <cfoutput> Format 1: <a href="dateformat.cfm?format=#URLEncodedFormat('dd/mm/yy')#">dd/mm/yy</a> (ex: 15/10/01) <br> Format 2: <a href="dateformat.cfm?format=#URLEncodedFormat('mmmm dd, yyyy')#">mmmm dd,  yyyy</a> (ex: October 15, 2001).<br> </cfoutput> 

    To append a variable to the end of a URL, you always start with a question mark. This separates the actual URL from any variables that are being appended to it. Anything that comes after the question mark will be URL variables, and not part of the URL used for location.

    You're also adding the URLEncodedFormat function around the parameters that you are passing. This makes the value safe to pass through the URL. Also, because you are using a ColdFusion function that manipulates a value and then outputs it, you must use the <cfoutput> tags.

    TIP

    If you want to pass more than one URL variable, you must separate them with ampersands (&). For example, if you want to pass a variable called lastname along with firstname it would look like this: <a href="firstname2.cfm?firstname=#firstname#&lastname=#lastname#> You can pass as many variables through the URL as you like.

    Now let's see how this looks when it's output.

  3. Save date.cfm. Open a browser and type http://127.0.0.1/Lesson3/Start/date.cfm into the address window and press Enter.

    When you open the browser, you will notice that the Today variable is now a linkthe one you created in step 1. When you pass your mouse over the link, look to the bottom of the browser window to see the address of the link. It should say:

    http://127.0.0.1/Lesson3/Start/dateformat.cfm?format=dd/mm/yy

    http://127.0.0.1/Lesson3/Start/dateformat.cfm?format=mmmm dd, yyyy.

    ColdFusion has formatted the values to be URL-safe. Now when the link is clicked, the variable format will be passed to the next template, dateformat.cfm, as you set it up in step 2. However, since you have not yet actually created dateformat.cfm, if you were to click the link at this point you would get an error.

    NOTE

    URL-safe means that all characters have been cleaned up, or changed, to accommodate being used in the URL. Spaces have been changed to plus signs, and other "foreign" characters have been changed to percent signs followed by the hexadecimal representations of those characters .

    Now we'll create that next template so the variable will have somewhere to go.

  4. Click File > Open and choose the dateformat.cfm file from the CD-ROM.

    The dateformat.cfm template will be used to display the URL variable that is being passed to it.

  5. Change the Dateformat function you created earlier to read:

     #Dateformat(today, "#URL.format#")# 

    You'll notice that you've preceded the format variable with the "prefix" URL. This is called scoping telling the ColdFusion Server specifically where to look for the variable. This is a good idea in general when you're outputting URL variables. The code will work without the URL prefix but ColdFusion would look at other variables form, session, and application variables as well as the URL variableuntil it found a match. Scoping the variable by including the prefix prevents this extra searching and therefore improves performance. Since you don't have a query that uses this value, you are just going to output the value for now. In subsequent lessons, you'll work with queries that actually use values that are passed through the URL.

  6. Press Shift+Ctrl+S and save the document as dateformat.cfm in the Inetpub\ wwwroot \Lesson3\Start folder. Open a Web browser and type http://127.0.0.1/Lesson3/Start/date.cfm into the address window and press Enter.

    The date.cfm template will load up with the link to the dateformat.cfm template with the variable Today appended to the URL.

  7. Click on the Format 2 link to the dateformat.cfm template.

    When the dateformat.cfm page loads, you should see "October 15, 2001" (without the quotation marks). As you can see, you passed the mask to the template and the date was formatted a little differently.

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