Search code examples
coldfusioncoldfusion-9

Passing Classes/Components with properties in Coldfusion


I'm new to CF, coming from a .NET background. I'm wondering what the best practice type thing would be for the following situation.

Say I have a component, car.cfc and I have a function inside this component that requires the properties:

<cfcomponent>
    <cfproperty name="Name" />
    <cfproperty name="Model" />
    <cfproperty name="Make" />

    <cffunction name="BuildCarXML">
        <cfargument name="car" type="car" />
        <cfsavecontent variable="xmlCar">
            <?xml version="1.0" encoding="UTF-8" ?>
            <car>
               <name>#arguments.car.Name#</name>
            </car>
        </cfsavecontent>
        <cfreturn xmlCar />
    </cffunction>
</cfcomponent>

Finally, i call this function from a cfm page:

<cfscript>
    cfcCar = CreateObject("car");
    cfcCar.Name="AU";
</cfscript>
<cfdump var="#cfcCar.BuildCarXML(cfcCar)#">

My question is, is this the correct/best way to do this?


Solution

  • <cfcomponent accessor="true">
        <cfproperty name="name" />
        <cfproperty name="model" />
        <cfproperty name="make" />
    
        <cffunction name="BuildCarXML" output="false">
            <cfsavecontent variable="local.xmlCar">
                <cfoutput><?xml version="1.0" encoding="UTF-8" ?>
                <car>
                   <name>#variables.name#</name>
                </car></cfoutput>
            </cfsavecontent>
            <cfreturn xmlCar />
        </cffunction>
    </cfcomponent>
    

    Finally, u call this function from a cfm page:

    <cfscript>
        cfcCar = new Car();
        cfcCar.setName("AU");
        writeDump(cfcCar.BuildCarXML());
    </cfscript>