User Guide

Table Of Contents
Using ColdFusion components 219
2.
Create a new ColdFusion page, with the following code and save it in the same directory as the
tellTime component:
<!--- Create the component instance. --->
<cfobject component="tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime"
returnvariable="localTime" >
<cfinvoke component="#tellTimeObj#" method="getUTCTime"
returnvariable="UTCTime" >
<!--- Display the results. --->
<h3>Time Display Page</h3>
<cfoutput>
Server's Local Time: #localTime#<br>
Calculated UTC Time: #UTCTime#
</cfoutput>
This example uses the cfobject tag to create an instance of the tellTime component and the
cfinvoke tag to invoke the instance’s getLocalTime and getUTCTime methods. In this example,
the CFC contains the functional logic in the methods, which return a result to the calling page,
and the calling page displays the results. This structure separates the logic from the display
functions, which usually results in more reusable code.
Invoking component methods transiently using the cfinvoke tag
In ColdFusion pages or components, the
cfinvoke tag can invoke component methods without
creating a persistent CFC instance.
To invoke a component method transiently, use the
cfinvoke tag and specify the following:
The name or path of the component, in the component attribute.
The method name, in the method attribute.
Any parameters. For information on passing parameters, see “Passing parameters to methods
using the cfinvoke tag” on page 225.
If the component method returns a result, the name of the variable that will contain the result,
in the
returnVariable attribute.
To invoke a component method using the cfinvoke tag:
1.
Create the following component and save it as tellTime.cfc:
<cfcomponent>
<cffunction name="getLocalTime">
<cfoutput>#TimeFormat(now())#</cfoutput>
</cffunction>
</cfcomponent>
The example defines a component with one method, getLocalTime, that displays the
current time.