Providing results



ColdFusion components can provide information in the following ways:

  • They can generate output that is displayed on the calling page.

  • They can return a variable.

You can use either technique, or a combination of both, in your applications. The best technique to use depends on your application’s needs and your coding methodologies. For example, many CFC methods that perform business logic return the results as a variable, and many CFC methods that display output directly are designed as modular units for generating output, and do not do business logic.

Displaying output

If you do not specifically suppress output, any text, HTML code, or output that CFML tags generate inside your method gets returned as generated output to the client that calls the component method. If the client is a web browser, it displays these results. For example, the following getLocalTime1 component method shows the local time directly on the page that invokes the method:

<cfcomponent> 
    <cffunction name="getLocalTime1"> 
        <cfoutput>#TimeFormat(now())#</cfoutput> 
    </cffunction> 
</cfcomponent>

Component methods that are called by using Flash Remoting or as web services cannot use this method to provide results.

Returning a results variable

In the component method definition, you use the cfreturn tag to return the results to the client as variable data. For example, the following getLocalTime2 component method returns the local time as a variable to the ColdFusion page or other client that invokes the method:

<cfcomponent> 
    <cffunction name="getLocalTime"> 
        <cfreturn TimeFormat(now())> 
    </cffunction> 
</cfcomponent>

The ColdFusion page or other client, such as a Flash application, that receives the result then uses the variable data as appropriate.

Note: If a CFC is invoked using a URL or by submitting a form, ColdFusion returns the variable as a WDDX packet. A CFC that is invoked by Flash Remoting, or any other instance of a CFC, must not return the This scope.

You can return values of all data types, including strings, integers, arrays, structures, and instances of CFCs. The cfreturn tag returns a single variable, as does the return CFScript statement. Therefore, if you want to return more than one result value at a time, use a structure. If you do not want to display output in a method, use output="false" in the cffunction tag.

For more information on using the cfreturn tag, see the CFML Reference.

Documenting CFCs

ColdFusion provides several ways to include documentation about your CFCs in your component definitions. The documentation is available when you use introspection to display information about the CFC or call the GetMetadata or GetComponentMetaData function to get the component’s metadata. You can use the following tools for documenting CFCs:

  • The displayname and hint attributes

  • User-defined metadata attributes

  • The cfproperty tag

For information on displaying the information, see Using introspection to get information about components.

The displayname and hint attributes

The cfcomponent, cffunction, cfargument, and cfproperty tags have displayname and hint attributes.

The displayname attribute lets you provide a more descriptive name for a component, attribute, method, or property. When you use introspection, this attribute appears in parentheses next to the component or method name, or on the parameter information line.

You use the hint attribute for longer descriptions of the component, method, or argument. In the introspection display, this attribute appears on a separate line or on several lines of the component or method description, and at the end of the argument description.

Metadata attributes

You can include arbitrary metadata information as attributes of the cfcomponent, cffunction, cfargument, and cfproperty tags. To create a metadata attribute, specify the metadata attribute name and its value. For example, in the following cfcomponent tag, the Author attribute is a metadata attribute. This attribute is not used as a function parameter; instead, it indicates who wrote this CFC.

<cfcomponent name="makeForm" Author="Bean Lapin">

Metadata attributes are not used by ColdFusion for processing; they also do not appear in standard ColdFusion introspection displays; however, you can access and display them by using the GetMetaData or GetComponentMetaData function to get the metadata. Each attribute name is a key in the metadata structure of the CFC element.

Metadata attributes are used for more than documentation. Your application can use the GetMetadata function to get the metadata attributes for a component instance, or the GetComponentMetaData function to get the metadata for an interface or component that you have not yet instantiated. You can then act based on the values. For example, a mathCFC component could have the following cfcomponent tag:

<cfcomponent displayname="Math Functions" MetaType="Float">

In this case, a ColdFusion page with the following code sets the MetaTypeInfo variable to Float:

<cfobject component="mathCFC" name="MathFuncs"> 
<cfset MetaTypeInfo=GetMetadata(MathFuncs).MetaType>
Note: All metadata values are replaced by strings in the metadata structure returned from the GetMetadata function. Because of this, do not use expressions in custom metadata attributes.

The cfproperty tag

The cfproperty tag is used to create complex data types with WSDL descriptors and for component property documentation, as follows:

  • It can create complex data types with WSDL descriptions for ColdFusion web services. For more information, see Using ColdFusion components to define data types for web services.

  • It can provide documentation of component properties in the ColdFusion introspection output. The introspection information includes the values of the standard cfproperty tag attributes.

Note: The cfproperty tag does not create a variable or assign it a value. It is used for information purposes only. You use a cfset tag, or CFScript assignment statement, to create the property and set its value.

Saving and naming ColdFusion components

The following table lists the locations in which you can save component files and how they can be accessed from each location:

 

URL

Form

Flash Remoting

Web services

ColdFusion page

Current directory

N/A

Yes

N/A

N/A

Yes

Web root

Yes

Yes

Yes

Yes

Yes

ColdFusion mappings

No

No

No

No

Yes

Custom tag roots

No

No

No

No

Yes

Note: ColdFusion mappings and custom tag roots can exist within the web root. If so, they are accessible to remote requests, including URL, form, Flash Remoting, and web services invocation.

When you store components in the same directory, they are members of a component package. You can group related CFCs into packages. Your application can refer to any component in a directory specifically by using a qualified component name that starts with a subdirectory of one of the accessible directories and uses a period to delimit each directory in the path to the directory that contains the component. For example, the following example is a qualified name of a component named price:

catalog.product.price

In this example, the price.cfc file must be in the catalog\product subdirectory of a directory that ColdFusion searches for components, as listed in the preceding table. When you refer to a component using the qualified name, ColdFusion looks for the component in the order described in Specifying the CFC location.

Establishing a descriptive naming convention is a good practice, especially if you plan to install the components as part of a packaged application.