Scope of Variables


Scope determines the lifetime of a variable in an application. In other words, it's the part of the application within which a variable has meaning or a value associated with it. When you create a variable, you can specify its scope by using the <CFINCLUDE> tag.

Some of the variable scopes, along with their prefixes, are listed in Table 2.2.

Table 2.2: Scope Types

Scope Type

Prefix

Description

Local

Variables

Includes variables created using the <CFSET> or <CFPARAM> tags. These variables can be specified with or without the scope prefix.

Form

Form

Includes data in tags in an HTML form or the ColdFusion <CFFORM> tag block.

URL

URL

Includes variables that are passed as URLs or query-string parameters to a page.

Query

Queryname

Accessible within a Query object.

CGI

CGI

Contains information about the server and browser environments in use. CGI variables are accessible to all ColdFusion templates.

Cookie

Cookie

Contains information about the values of HTTP cookies retrieved from a user's Web browser.

Client

Client

Contains data about a particular user.

Session

Session

Contains data pertaining to a particular session.

Application

Application

Contains application-wide information, such as the default directory.

Server

Server

Stores data about the server on which ColdFusion is running.

The scope of variables is discussed in detail in the following sections.

URL

URL parameters are a very powerful means to make a Web document dynamic by passing values to a Web page through the URL. A URL parameter is passed using the name and value of the parameter, as shown in the following example:

 "http://myhost/mypage.cfm?MyValue=100." 

Here, ? is used to separate the standard URL from the parameters you're passing.

If more than one parameter needs to be passed, you can use an ampersand as a separator between the two:

 http://myhome/mypage.cfm?Var1=green&Var2=yellow 

The preceding code creates two parameters, Var1 with a value green and Var2 with a value yellow. The parameter values can be accessed using this syntax:

 URL.parameter_name 

It's difficult for users to remember complicated URLs that contain parameters. Therefore, you can use URL parameters with the anchor tag (<A>), as follows:

 <A HREF="incometax.cfm?TaxRate=0.05"> Minnesota </A> 

Common Gateway Interface

Common Gateway Interface (CGI) variables are created when a template is requested and returned to a browser. Templates can contain HTML, ColdFusion tags, and functions. ColdFusion templates are plain-text files, similar to HTML files. Unlike HTML files, which are sent to the browser, templates are processed by ColdFusion first. This way, you can embed instructions to be processed by ColdFusion within your templates. CGI variables contain the information about the server and browser environments in use.

Note

The CGI variables are read-only, and therefore the <CFSET> tag cannot be used to assign values to CGI variables.

Cookies

Cookies are variables that contain information stored by the server in a client browser. The <CFSET> tag cannot be used to set cookies. Cookie variables can be created by using the <CFCOOKIE> tag. When you create the cookie, you need to specify the name, value, security status, expiry date, path, and domain name. However, "empty" cookies can be created with no value assigned. Except for the cookie name, all the other attributes are optional. You can access cookies by adding the Cookie prefix to the name of the cookie.

Queries

Queries are used to retrieve information from a database. The <CFQUERY> tag, in conjunction with SQL statements, can be used for information retrieval. Some of the commonly used attributes that the <CFQUERY> tag uses are as follows:

  • NAME contains the name of the query.

  • DATASOURCE contains the name of the data source to be accessed.

  • DBTYPE contains the database driver being used.

Forms

Forms are used to create interactive Web-based applications. The <CFFORM> tag is used to create forms and is similar to the HTML <FORM> tag.

The <CFFORM> tag takes the following attributes:

  • ACTION. Contains the file name of the template to which the form needs to be submitted

  • ENCTYPE. Contains the MIME type of the data

  • ENABLECAB. Indicates whether Java controls should be made available

  • NAME. Contains the name of the form

  • ONSUBMIT. Serves as the function that executes after the form validation is complete

  • TARGET. Contains the name of the frame or window in which the file specified by the ACTION attribute should be opened

  • PASSTHROUGH HTML. Used for attributes that are not explicitly supported by the <CFFORM> tag

There are other related tags, such as <CFINPUT>, <CFSELECT>, <CFGRID>, and <CFTREE>. The <CFINPUT> tag provides a way to create dynamically populated and automatically validated fields. The <CFSELECT> tag is used to create dynamic select fields.

Client, Session, Application, and Server Variables

Client, session, application, and server variables are related to the ColdFusion application framework. They're used to maintain state over a series of templates.

The client variable has a one-to-one relationship with a single client and can persist over multiple sessions. ColdFusion creates a client variable using the name of the application specified with the <CFAPPLICATION> tag and with two cookies, CFID and CFTOKEN.

Session variables exist during a single session for a client. They have a one-to-one relationship with a particular session. A name must be specified in the <CFAPPLICATION> tag by using the NAME attribute. Session variables are created by using the <CFSET> or <CFPARAM> tags. Whenever a session variable is read or written, it needs to be enclosed within a <CFLOCK> tag. This locks the variable so that it isn't used by other applications simultaneously.

Application variables have a one-to-one relationship with a particular application. However, application variables are accessible by multiple clients. They're used to store information needed for all clients of a particular application. Creating application variables is similar to creating session variables. However, the name prefix is different and the variable needs to be locked. <CFSET> and <CFPARAM> tags can be used to create application variables. To access application variables, the Application prefix needs to be used. The format is as follows:

 Application.variable_name 

Server variables have a one-to-one relationship with a ColdFusion server. They're not tied to any specific application and can be accessed by all clients and applications on that server. The <CFSET> and <CFPARAM> tags can be used to create a server variable. These variables must be locked when created. To access these variables, they should be prefixed with Server.

Order of Evaluation of Scope

Most of the variables don't require the specification of the scope. When a variable with no defined scope appears within a ColdFusion template, ColdFusion attempts to determine the scope through a predefined evaluation order, as follows:

  1. Local variables

  2. CGI variables

  3. URL variables

  4. Form variables

  5. Cookie variables

  6. Client variables

Note

Application, server, and session variables are always to be scoped. Therefore, they're not mentioned in the preceding list.

To understand how the order of evaluation is decided, consider the following example. A template has been passed to a URL variable called MyTest. In addition to this parameter, the template receives another cookie variable with the same name, MyTest. When you use #MyTest# in the template, it evaluates to the value of URL.MyTest because the URL scope is first in the evaluation order. Therefore, there's no clear manner in which you can refer to Cookie.MyTest without specifying the cookie scope. In addition, let's say you create a local variable named MyTest using the <CFSET> tag. Because of the predefined evaluation order, #MyTest# evaluates to the MyTest variable created using the <CFSET> tag. It is a good practice to add the scope identifier prior to the variables so that performance is enhanced because it avoids searching for the variables.




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

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