Building ColdFusion components

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.

Syntax for the cfcomponent tag

<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.

Syntax for the cffunction tag

<cffunction name="methodName" returnType="dataType" 
  roles="securityRoles" access="methodAccess" output="yes/no">

The following table displays the tag attribute, data type, and description:
Attribute
Type
Required
Description
For more information
name
string
yes
Name of component method
returnType
string
no
Data type validation for returned values.
roles
string
no
Assigns component method to ColdFusion security roles.
access
string
no
Restricts component method access by client type.
output
Boolean
no
Suppresses component method output

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".

Defining 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.

To create a component method:

  1. Create a new ColdFusion component, and save it as tellTime.cfc in a directory below your web-root directory.
  2. Modify the code so that it appears as follows:
    <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.

  3. Save your work.

By placing the method execution code in a separate file, template methods separate execution and markup code from the component method definitions.

To create component method using the cfinclude tag:

  1. Open the tellTime.cfc file, and modify the code so that it appears as follows:
    <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.

  2. Save your work.
  3. Create a ColdFusion page, and save it as getTime.cfm in the same directory as tellTime.cfc.
  4. Modify getTime.cfm so that the code appears as follows:
    <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.

  5. Save your work.

Comments