Using cfscripts

just tring to learn a bit more about cfscrips and wanted to know if i was using them in the right way ie

hope this makes sense..............


.cfm page
----------------------------

<cfscript>
a function
</c\script>

code that calls script function above ie <cfset array = parsesomestuff(cfhttp.FileContent)>

------------------------------





i was thinking is it possible to do it something like this instead of the above on one page...

.cfc
------------------------------
<cfscripts>
a function
</cfscripts>
----------------------------

.cfm
call function from .cfc
use it <cfset array = parsesomestuff(cfhttp.FileContent)>
------------------------------

? thanks inadvance
LVL 12
pigmentartsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mkishlineCommented:
You can call <cfscript> functions from a cfc onto a cfm page. The syntax will the same as if you were calling a cffunction.

Either:

<cfinvoke component="component.path" method="parsesomestuff" returnvariable="array" />

Or

<cfset componentObj = CreateObject("component","component.path") />
<cfset array = componentObj.parsesomestuff(cfhttp.FileContent) />

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
incapitalCommented:
The syntax for creating and invoking a a component in a cfc is different from the syntax for creating a simple udf with cfscript. The following page has some syntax examples you may find useful:

http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00001041.htm

If you want to use the syntax in your example, you should just create your function as a UDF (user-defined function), and include it in the pages where you'll be using it.

pigmentartsAuthor Commented:
So is a user defined function better practice than what I suggested?
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

mkishlineCommented:
It all depends on how many different places you intend to use the function. If you're going to use the function only on one page, you should just leave the <cfscript> code on one page. If you would rather have the code available to use on more than one page, you should include it in a cfc (either as a cfscript or as a cffunction) and refer to it from wherever you would like.
incapitalCommented:
Not better, per se, just different. A UDF will support the syntax you supplied in your example. If you want to use a CFC instead, you should look at the documentation in the link I included, and then use the syntax supplied by mkishline.
pigmentartsAuthor Commented:
sound good u guys no ur stuff. so then  if i was going to use this script more than once (different pages),  cfc best, but if its one page, why bother, i got it.


pigmentartsAuthor Commented:
so in a page a function like this would work, then i just use than function?

.cfm

<cfscript>
function
{

}
</cfscript>
mkishlineCommented:
Yes, so long as you define the function before you call it.
incapitalCommented:
Yes, that would work. You just need to sure that the function is available to the page that requests it. You can do this by pasting the function code onto the page, or by including the function through a cfinclude. Some folks put their UDFs in their application.cfm file, though I'm not crazy about doing that.
pigmentartsAuthor Commented:
i may have miss understood this, but if a function (object) like the cfscript is on the same page does it need to be defined becuase it is not used until i do something time it, or am i not getting this right?

ie

.cfm page

------------------

<cfscript>
function object
{

}
</cfscript>

same .cfm
<cfset array = my-function-above(by using something here)>
pigmentartsAuthor Commented:
sorry just read the post above by me back, bad spelling, hope it made sense
mkishlineCommented:
All you need to make sure of is that you have

<cfscript>
  function object{
  }
</cfscript>

and then further down the page:

<cfset array = my-function-above(arguments) />
pigmentartsAuthor Commented:
thaks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.