Chapter 52: Using Client, Session, and Application Variables

 < Day Day Up > 



 Download CD Content

The applications you've built in the previous chapters have all consisted of only a page or two. But as you venture further into ColdFusion, you'll begin to develop more complex applications that span multiple pages. In these cases, it's helpful to consider your applications as a whole rather than a collection of individual templates. When you formally define a group of templates as an application, you gain access to a powerful set of tools allowing you to easily share variable data from one page to another — without having to pass variables in a URL or form. These tools also enable you to create variables that persist for a fixed duration, from one user session to another, or for as long as your application remains running on your server.

Using Application.cfm Pages

The key to grouping your templates together as applications is a unique file called  Application.cfm. You create this file yourself, usually before you begin developing other templates in an application. The file functions in much the same way as .ini files, which may be familiar to Windows and Unix users: It defines parameters that are used throughout an application.

 Application.cfm files are always stored in the uppermost folder of your site. When they're present, their contents are automatically included in every ColdFusion template that lies within their folder. The same applies for files stored in folders below the one containing  Application.cfm, as long as those folders don't contain  Application.cfm files of their own. Take a look at Listing 52-1 to see a sample  Application.cfm file.

Listing 52-1: A simple Application.cfm file

start example
<cfapplication name="my_web_store">     <cfset dsn = "store_datasource"> <cfset store_name = "My Web Store">
end example

The file in Listing 52-1 is a simple one. It defines the application with the <cfapplication> tag; then it defines a couple of variables that will be used throughout every template in the application. Since the contents of this page will automatically be inserted into every template residing in this file's folder and those below it, you could then use code like this to define a query:

<cfquery name="some_query" datasource="#dsn#"> select * from users </cfquery>

Instead of supplying the actual name of a datasource in your <cfquery> tag, you can use the variable value #dsn#, which is defined in  Application.cfm. Why is this valuable? Let's say that a few months after you've developed your application you decide to move it to another server, one that requires a different name for your datasource. Rather than go through all of your templates that include <cfquery> to change the name of the datasource, you can simply change the value of the variable in  Application.cfm once and be finished.

The same applies for store_name in this example. By defining it in  Application.cfm and then using the variable #store_name# in your templates, rather than the actual name of the store, you make it easy to change the name at a later date.

start sidebar
Using <cfinclude>

Another way of inserting code from an external file is the <cfinclude> tag. It takes one parameter — template — which contains the path name or filename containing the code to be inserted. For example, to create a header section that is consistent throughout your site, you could save the HTML and/or CFML code in an external file named header.cfm and then add some code like this to all of the pages in your site, wherever you want the header code to display:

<cfinclude template="header.cfm">

If the header template is stored in a folder other than the one using the <cfinclude>, you need to specify the file path:

<cfinclude template="../../header.cfm"> 

Code that's inserted by a <cfinclude> tag is processed exactly as if it had been typed into the file. <cfinclude> can be an enormous timesaver for headers, footers, or other code that repeats frequently throughout your site.

end sidebar

Note 

The variables dsn and store_name are just examples of how variables defined in  Application.cfm are available to any template within the application (with the exception of custom tags and ColdFusion components, as you'll learn later in this section). You can use any variable names you'd like: In fact many complex applications define scores of variables in their  Application.cfm files.

The value of  Application.cfm files goes far beyond their ability to insert code into templates, however. In the next sections you'll learn how using  Application.cfm to formally define your applications.



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

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