Just like ColdFusion pages, you store component files in a domain accessible by your web server and ColdFusion. Unlike ColdFusion pages, you save component files with the CFC suffix, such as componentName.cfc.
Save your component files in one of the following locations:
Note: For more information about saving components and component naming conventions, see "Using component packages".
All ColdFusion variable scopes are available to components, including Session
, Client
, Server
, and Application
. In addition, the This
scope is available during component method execution.
You use the cfcomponent
and cffunction
tags to create ColdFusion components. By itself, the cfcomponent
tag does not provide functionality. Rather, the cfcomponent
tag provides an envelope that describes the functionality that you build in CMFL and enclose in cffunction
tags.
<cfcomponent extends="anotherComponent">
The following table displays the tag attribute, data type, and description:
Attribute |
Type |
Required |
Description |
For more information |
---|---|---|---|---|
extends |
string |
no |
Name of parent component. |
Note: The cfcomponent tag is optional.
<cffunction name="methodName" returnType="dataType" roles="securityRoles" access="methodAccess" output="yes/no">
The following table displays the tag attribute, data type, and description:
The following example creates a component with two methods:
<cfcomponent>
<cffunction name="getEmp"> <cfquery name="empQuery" datasource="ExampleApps" dbtype="ODBC" > SELECT FIRSTNAME, LASTNAME, EMAIL FROM tblEmployees </cfquery> <cfreturn empQuery> </cffunction> <cffunction name="getDept"> <cfquery name="deptQuery" datasource="ExampleApps" dbtype="ODBC" > SELECT * FROM tblDepartments </cfquery> <cfreturn deptQuery> </cffunction> </cfcomponent>
In the example, two cffunction
tags define two component methods, getEmp
and getDept
. When invoked, the component methods query the ExampleApps database. The cfreturn
tag returns the query results to the client. For more information, see "Invoking component methods".
Component method definitions exist between opening and closing cffunction
tags. To separate the component method code from the component file, use the cfinclude
tag to call the page that contains the component method code.
<cfcomponent> <cffunction name="getLocalTime"> <cfscript> serverTime=now(); localStructure=structNew(); localStructure.Hour=DatePart("h", serverTime); localStructure.Minute=DatePart("n", serverTime); </cfscript> <cfoutput> #localStructure.Hour#:#localStructure.Minute# </cfoutput> </cffunction> </cfcomponent>
In the example, the cfscript
and cfoutput
statements execute during component method processing.
By placing the method execution code in a separate file, template methods separate execution and markup code from the component method definitions.
<cfcomponent> <cffunction name="getLocalTime"> <cfinclude template="getTime.cfm"> </cffunction> </cfcomponent>
In the example, the getLocalTime
method definition calls the getTime.cfm file with the cfinclude
tag.
<cfscript> serverTime=now(); localStruct=structNew(); localStruct.Hour=DatePart("h", serverTime); localStruct.Minute=DatePart("n", serverTime); </cfscript> <cfoutput>#localStruct.Hour#:#localStruct.Minute#</cfoutput>
In the example, a CFScript statement uses the now()
and DatePart()
functions to populate a structure with hour and minute values. The values are then displayed with the cfoutput
tag. Notice that no value is returned to the client. Instead, the getTime method displays the variable.