ColdFusion® MX: From Static to Dynamic in 10 Steps By Barry Moore
Table of Contents
Step 8. ColdFusion Application Framework
In this example, we are going to create an Application.cfm file. We will then set a variable in the Application.cfm file and use that variable in another template.
Open your text editor and type the code in Listing 8.1 or open the completed Application.cfm file from the CompletedFiles\Examples\Step08 folder.
Listing 8.1 Application.cfm
<!--- File: Application.cfm Description: Settings for MyCompanyApp application Author: Created: ---> <!--- Name application, enable session management and set session time out to 15 minutes ---> <CFAPPLICATION NAME="MyCompanyApp" SESSIONMANAGEMENT="Yes" SESSIONTIMEOUT="#CreateTimeSpan(0,0,15,0)#"> <!--- Set the DSN to be used in this application ---> <CFSET MyAppDSN="Staff">
Save this file as Application.cfm in your Examples\Step08 folder.
In your Examples\Step08 folder, you will find a file called EmployeeList.cfm. This is the same file we created in Step 3, "Databases and SQL," to list all employees in the staff database.
With your text editor, open the EmployeeList.cfm file from the Examples\Step08 folder.
We want to use the new MyAppDSN variable that we just defined in the Application.cfm file. Find the query at the top of the EmployeeList.cfm template and replace the "Staff" text in the <CFQUERY> tag with "#MyAppDSN#", as follows:
<!--- retrieve all employees and extensions using the new variable set in Application.cfm---> <CFQUERY NAME="qStaffList" DATASOURCE="#MyAppDSN#"> SELECT FirstName, LastName, Extension FROM Employees ORDER BY LastName </CFQUERY>
Save the file and browse to it using your browser. You should get the same employee listing from Step 3, but by using a variable for our DSN instead of typing it in each time, we have made our application much more portable.