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?
 
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) />
0
 
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.

0
 
pigmentartsAuthor Commented:
So is a user defined function better practice than what I suggested?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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.
0
 
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.
0
 
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.


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

.cfm

<cfscript>
function
{

}
</cfscript>
0
 
mkishlineCommented:
Yes, so long as you define the function before you call it.
0
 
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.
0
 
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)>
0
 
pigmentartsAuthor Commented:
sorry just read the post above by me back, bad spelling, hope it made sense
0
 
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) />
0
 
pigmentartsAuthor Commented:
thaks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.