Creating Local Variables

I l @ ve RuBoard

Creating Local Variables

You're going to use your new knowledge of variables to add and format the current date so that it always appears on the front screen of the Web site. Creating this element is a great way to learn how to work with a simple and common variable, and also with several different functions. The shell of the site has already been set up, so now we'll use simple variables and functions to add the date component.

Local variables are the simplest type of ColdFusion variables. They are available to use only in the template in which they are created. Once ColdFusion has finished processing the template, the variables are gone and you can no longer use them. Being able to create these types of variables is essential to developing your ColdFusion templates. Fortunately, creating a local variable is easy.

In the following task, you'll create a ColdFusion document that uses a local variable. The variable can be referenced anywhere on the template in which it is created.

In the Wireless Agent home page pictured at the beginning of this lesson, you may have noticed the date that appears in the upper-right corner of the page. We are now going to backtrack into the design of that page and add the date to the template. We'll also format the date. The shell of the site has already been set up.

Creating this element is a good way to learn how to work with a simple and common variable, and also with several different functions.

NOTE

Posting the current date is one technique to help make the content on your site appear fresh and up to date.


  1. In the Inetpub/ wwwroot folder, create a new folder named Lesson3. In the Lesson3 folder, create a folder named Start. Move the style sheet, Images folder and date.cfm file from the CD to Lesson3/Start on your hard drive.

    You must have these files in your wwwroot folder for the ColdFusion template to process properly. If you don't, you won't get the intended result.

  2. Open ColdFusion Studio and choose File > Open . Select the Lesson3\Start\date.cfm template from the CD-ROM and click OK.

    The date.cfm template will appear in the editor window.

    TIP

    If you would like to look at the file you're about to work on, open a Web browser, type http://127.0.0.1/Lesson3/Start/date.cfm into the address window, and press Enter. You should see the Wireless Agent Web site, without the date in the upper-right. You're about to add the date using a simple local variable.

  3. At the top of the page, type <cfset Today = Now()> .

    You will have to add some carriage returns to the top of the page in order to make room for the code. You have now created a variable called Today, and it holds the value of the current date.

    NOTE

    ColdFusion variables are typeless, which means that they do not have to be declared or created before you assign a value to them.

    TIP

    When you name your variables, it is a good idea to make the names obviously meaningful. With the variable Today there is little doubt as to what piece of information the variable holds. However, if you had made the variable name Date, you might be confused later. Variable names can contain numbers and letters but not spaces. If you want to separate values in your variables, use underscores. In the example above, you could have named the variable Todays_Date to separate the values.

  4. On line 50 of the date.cfm template, type <cfoutput> #Today#</cfoutput> .

    The <cfoutput> tag is one of the most commonly used tags in ColdFusion. Any time you want ColdFusion to display information, such as a variable, you must place it within <cfoutput> tags, as shown below.

    Also, when you are setting up the code for displaying or outputting the value stored in a variable, the variable must be surrounded by # symbols. This lets the ColdFusion server know that it needs to evaluate this variable and return the value it contains. Any information that is contained in a <cfoutput> tag will be processed by the ColdFusion server whether it is ColdFusion code or plain HTML.

  5. Select File > Save As.

    TIP

    You can also use the shortcut commands Ctrl+S and Ctrl+Shift+S to perform Save and Save As functions, respectively.

  6. Open a Web browser and type http://127.0.0.1/Lesson3/Start/date.cfm into the address window, and then press Enter.

    ColdFusion has processed the variable Today and returned the value {ts '2001-10-15 18:49:54'} along with the HTML content of the template. This doesn't look like a normal date. However, this is how ColdFusion returns the value on its own (that is, with no further formatting or functions involved). You've successfully created the variable! However, you will need to use the dateformat() function to make it look right. We'll do that in the next task.

    NOTE

    Your dates and times don't match the ones shown here. The date and time should be the current date and time, from your computer, or whichever machine you are running the ColdFusion server on. ColdFusion uses those times when the Now() function is called.

  7. Select View > Source.

    When you view the source code, scroll down and you will notice among the HTML that the ColdFusion code is goneall that remains is the file with the variable value right where it should be. That is all it takes to create and output simple local variables. Don't close date.cfm yet.

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