Hi everyone!! I am using cfm files and i have never used cfc; i ve read about cfc but i could not figure out how it works?

could i use this cfc files in my cfm? and can you give example like calling an xml file.

Recommended Answers

NO you dont do this only on one way you know better and you understand very good

Jump to Post

All 4 Replies

CFC is a seperate file. And you can save a cfc file with an extension of (.cfc). In cfc files you can create several methods (have restriction to number of methods in a cfc, which is according to JVM). And we can create an object for the cfc (ie a component file) and with this object by calling any methods in that particular cfc you can perform any manipulation on those methods. And you can change your presentation part using the buisiness logics used inside the cfc.

I have written an example to understand the working of cfc, Please see it below

File : displayactors.cfm

<cfset XMLOperationObject = CreateObject("component","XMLOperation")>
<cfxml variable="XMLString">
		<actor name="Tom Crus" dob="14 September 1982"></actor>
		<actor name="Briana Banks" dob="21 May 1978"></actor>
		<actor name="Tawny Roberts" dob="11 March 1959"></actor>

<cfset ActorsNames = XMLOperationObject.ReadXML(XMLString)>

File : xmloperation.cfc

<cfcomponent name="XMLOperation" output="false">
	<cffunction name="ReadXML" access="public" returntype="Array" hint="To validate an XML">
		<cfargument name="InputXML" required="true" type="string" hint="XML to be read">
				var XMLString = "";
				var ActorsName = ArrayNew(1);
			<cfloop index="NodeIndex" from="1" to="#ArrayLen(ARGUMENTS.InputXML.actors.actor)#">
					XMLActors = ARGUMENTS.InputXML.actors.actor[NodeIndex];
					ActorsName[NodeIndex] = XMLActors.XMLAttributes.name;
		<cfreturn ActorsName>

In displayactors.cfm I am creating an instance for the component xmloperation.cfc.With this object I am calling the method ReadXML, which reads the xml and find the name attribute corresponding to each actor node and return the names as an array (You can view this by dumping the variable ActorsNames). In cfm file you can use this array for any purpose according to your need , say displaying actors name in a form.

NO you dont do this only on one way you know better and you understand very good

I also use .cfc file...in particular application.cfc to define my application variables such as my site wide datasource.

<cfcomponent output="false">

  <!--- Name the application. --->
  <cfset this.name="OrangeWhipSite">
  <!--- Turn on session management. --->
  <cfset this.sessionManagement=true>
  <cffunction name="onApplicationStart" output="false" returnType="void">

    <!--- Any variables set here can be used by all our pages --->
    <cfset APPLICATION.dataSource = "ows">
    <cfset APPLICATION.companyName = "Orange Whip Studios">


Yo can use application.cfc to declare all global and session application variabales

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.