View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfcomponent PreviousNext

cfcomponent

Creates and defines a component object; encloses functionality that you build in CFML and enclose within cffunction tags. This tag contains one or more cffunction tags that define methods. Code within the body of this tag, other than cffunction tags, is executed when the component is instantiated.

A component file has the extension CFC and is stored in any directory of an application.

A component method is invoked in the following ways:

Extensibility tags

<cfcomponent
extends ="anotherComponent"
output = "yes" or "no"
displayname = "text string">
hint = "text string"> variable declarations
<cffunction ...>
...
</cffunction>

<cffunction ...>
...
</cffunction>
</cfcomponent>

cfargument, cffunction, cfinvoke, cfinvokeargument, cfobject, cfproperty, cfreturn, Building and Using ColdFusion Components in Developing ColdFusion MX Applications

Attribute

Req/Opt

Default

Description

extends

Optional

 

Name of parent component from which to inherit methods and properties.

output

Optional

Component body displayable text is processed as standard CFML

Specifies whether constructor code in the component can generate HTML output; does not affect output in the body of cffunction tags in the component.

  • yes: Constructor code is processed as if it were within a cfoutput tag. Variable names surrounded by number signs (#) are automatically replaced with their values.
  • no: Constructor code is processed as if it were within a cfsilent tag.
  • If you do not specify this attribute, constructor code is processed as standard CFML. Any variables must be in cfoutput tags.

displayname

Optional

name attribute value

A string to be displayed when using introspection to show information about the CFC. The information appears on the heading, following the component name.

hint

Optional

 

Text to be displayed when using introspection to show information about the CFC. The hint attribute value appears below the component name heading. This attribute can be useful for describing the purpose of the parameter.

If you specify the extends attribute, the data and methods of the parent component are available to any as if they were parts of the current component. If the managerCFC component extends the employeeCFC component, and the employeeCFC component has a getEmployeeName method, you can call this method using the managerCFC, as follows:

<cfinvoke component="managerCFC" method="getEmployeeName" returnVariable="managerName" EmployeeID=#EmpID#>

This tag requires an end tag.

<cfcomponent>
   <cffunction name="getEmp">
       <cfquery 
name="empQuery" datasource="ExampleApps" > SELECT FIRSTNAME, LASTNAME, EMAIL FROM tblEmployees </cfquery> <cfreturn empQuery> </cffunction> <cffunction name="getDept"> <cfquery
name="deptQuery" datasource="ExampleApps" > SELECT * FROM tblDepartments </cfquery> <cfreturn deptQuery> </cffunction> </cfcomponent>

Contents > CFML Reference > ColdFusion Tags > cfcomponent PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


wlee said on Mar 24, 2004 at 10:33 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

<cfcomponent>
<cffunction name="getEmp">
<cfquery name="empQuery" datasource="ExampleApps" >
SELECT FIRSTNAME, LASTNAME, EMAIL
FROM tblEmployees
</cfquery>
<cfreturn empQuery>
</cffunction>

<cffunction name="getDept">
<cfquery name="deptQuery" datasource="ExampleApps" >
SELECT *
FROM tblDepartments
</cfquery>
<cfreturn deptQuery>
</cffunction>
</cfcomponent>

Assuming you save the code above in a file called managerCFC.cfc, you can invoke the component and view the return variable from another page with this code:

<cfinvoke component="managerCFC" method="getEmp" returnVariable="employees">
<cfdump var="#employees#">
TomChiv said on Mar 26, 2004 at 6:20 AM :
>any comments on this example
You should include an example of creating a CFC:
<webroot>/com/myhost/myapp/example.cfc
and invoking as
com.myhost.myapp.example.function(args="an arg");
or similar.

Also using a CFC as a type in cfargument.
n8shaw said on Apr 1, 2004 at 9:22 AM :
I would like to see information about how a cfc is constructed (can you include a constructor method?) and how variables inside the cfc are treated (are they accessible via calling pages or not?).
jrunrandy said on Apr 1, 2004 at 9:27 AM :
Constructor info: http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/buildi36.htm
Scoping: Go to http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/buildi45.htm and click the right arrow key through the pages.
No screen name said on Aug 6, 2004 at 1:35 AM :
EXTENDS is case-sensitive. We ran into this while changed the mach-ii core files to lowercase.
While this is not a bug, it's an inconvenience and should use the same algorithm than CFINVOKE:
"On UNIX systems, ColdFusion searches first for a file with a name that matches the specified component name, but is all lower case. If it does not find the file, it looks for a file name that matches the component name exactly, with the identical character casing."
jsevlie said on Aug 26, 2004 at 11:49 AM :
Can you use a list for the EXTENDS parameter? Can I have a component inherit from multiple parent classes?
TomChiv said on Dec 9, 2004 at 1:31 AM :
The list of attributes says
"displayname Optional name attribute value"
but the 'name' attribute is missing from the table
No screen name said on Sep 21, 2005 at 12:11 AM :
This example doesn't help. I am confused with cfscript and cffunction and I can't find any infomation to solve my problems in this site.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-a16.htm