Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we replace every ColdFusion tag by equivalent cfscript statements?

I wanted to know can my cfml page or cfc components with with only cfscript tag?

Can we use it everywhere? Is there any limitation in its usage?

Edit:

I am curious because I read the following line

In addition to variable setting, other operations tend to be slightly faster in CFScript than in tags.

Read it here

like image 207
Vikas Avatar asked Dec 28 '22 15:12

Vikas


2 Answers

Most tags are now implemented as CFScript-ready implementations, but not all of them. Contrary to what the previous poster said, CFMAIL is one of the ones that has already been done: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSe9cbe5cf462523a0693d5dae123bcd28f6d-7ff9.html

As far as the other script coverage goes, it's in the docs: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7feb.html

Note, one can definitely write CFCs entirely in script now: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSE99A664D-44E3-44d1-92A0-5FDF8D82B55C.html

But I would be cautious about doing this because not all tags are implemented in script yet, and if you suddenly find you need too use one of them in your script-only CFC... you're a bit stuck.

Also I think some constructs like CFQUERY are a more elegant solution than Query.cfc's approach.

As for the comment that CFScript is faster than tag-based code, that hasn't really been the case since the compiler changes in CFMX7.0. Mostly the code compiles down to pretty much the same thing now. Some operations are faster in CFScript, some are faster in tag-based code. That said, these performance gains are going to be minimal compared to tuning your actual code or DB access or memory handling: I'd not refactor tag-based code to script-based code to try to find performance gains.

like image 52
Adam Cameron Avatar answered Jan 13 '23 10:01

Adam Cameron


As of CF11, all cf* tags are supported in cfscript.

General format is like this:

<!--- tag version --->
<cfwhatever arg1="val1" arg2="val2" ... />

<!--- script version --->
<cfscript>
  cfwhatever(arg1="val1", arg2="val2", ...);
</cfscript>

When you have nested tags (i.e. cfhttp/cfhttpparam), the format is like-

<!--- tag version --->
<cfwhatever arg1="val1" arg2="val2" ...>
  <cfwhateverparam arg3="val3" ... />
</cfwhatever>

<!--- script version --->
<cfscript>
  cfwhatever(arg1="val1", arg2="val2", ...) {
    cfwhateverparam(arg3="val3", ...);
  };
</cfscript>

Gotchas

I think I remember (though I haven't found documentation for this) that some cf* tags are not supported if they already had a cfscript alternative prior to CF11.

CF tags used as functions do not return a value, and will generate an error if you try to use them that way:

<cfscript>
  cfwhatever(arg1="val1", arg2="val2", ...); //THIS IS OK

  var myresult = cfwhatever(arg1="val1", arg2="val2", ...); //SYNTAX ERROR!

  //generally, this is what you do instead:
  var myresult = '';
  cfwhatever(arg1="val1", arg2="val2", ..., variable="myresult");
</cfscript>
like image 44
Kip Avatar answered Jan 13 '23 11:01

Kip