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.

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">
	<actors>
		<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>
	</actors>
</cfxml>

<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">
			<cfscript>
				var XMLString = "";
				var ActorsName = ArrayNew(1);
			</cfscript>
			
			<cfloop index="NodeIndex" from="1" to="#ArrayLen(ARGUMENTS.InputXML.actors.actor)#">
				<cfscript>
					XMLActors = ARGUMENTS.InputXML.actors.actor[NodeIndex];
					ActorsName[NodeIndex] = XMLActors.XMLAttributes.name;
				</cfscript>			
			</cfloop>		
		<cfreturn ActorsName>
	</cffunction>
</cfcomponent>

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">
  
  </cffunction>

  
</cfcomponent>

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.