
ColdFusion MX 7 Getting Started Experience Tutorial Page 43 of 47
Figure 61. Saving the page
as art.cfc
3. Modify the default CFC template to better reflect your needs. Start by setting the returntype attribute of the
cffunction tag to query, and naming the function getArtwork. Having the access attribute set to public
means that any server can access this function.
4. Delete the cfargument tag, because you do not need it for this function.
5. Modify the cfset tag so that it reads <cfset var artwork="">.
Learning Point: Creating variables
You use the cfset tag to create variables; for example:
<cfset firstName="Bart">
<cfset lastName="Simpson">
You use the cfset tag in the CFC to create, or initialize, the artwork variable. Recall that artwork is also the name of
the query on the index.cfm page. You are going to move the query from the index.cfm page into this function, and the
best practice for naming queries inside of functions is to first initialize them using the cfset tag. In other words, the
artwork variable you just created in the cfset tag is actually just a preparatory space for the query that will eventually
populate it.
6. Update the cfreturn tag so that it references the artwork variable instead of the myResults variable, as the
following code shows:
<cfcomponent>
<cffunction name="getArtwork" access="public" returntype="query">
<cfset artwork="">
<cfreturn artwork>
</cffunction>
</cfcomponent>
Learning Point: CFM and CFC relationship
As mentioned earlier, CFCs only contain the business logic for an application. From the index.cfm page, you call the
getArtwork function, to request that the data be returned to that page. The cfreturn tag is vital for this purpose. In
this example, it is set to return the variable, artwork, which you declared in the cffunction tag’s returntype
attribute as a query.
Kommentare zu diesen Handbüchern