November 30, 2017
Here’s how you provide CFFUNCTION tag attributes when converting it to a script function
November 30, 2017
Here’s how you provide CFFUNCTION tag attributes when converting it to a script function
ColdFusion troubleshooter
Wizard 146 posts
Followers: 111 people

Have you needed to convert a CFFUNCTION to a script equivalent function or CFC method, and wondered how you would specify the things which had been attributes to the tag, such as OUTPUT, ROLES, RETURNFORMAT (added in CF8), or RESTPATH (added in CF10), to name just a few? It’s just not obvious.

The quick answer is that you specify them AFTER the closing parenthesis which surrounds any arguments to the function. So, for example:

public numeric testMethod (numeric arg1) returnFormat="json" output="false {
//method body

As always, there’s a bit more about this which you may want to consider, and I’ll discuss it further here.

Setting the stage: CF11 added support for all tags as script

First, as most will know by now, you can write your CFML entirely in script if you want, since CF11. And the approach is generally that you take any tag and turn it into a method name itself, putting whatever attributes the tag had in parentheses after the tag name. So for instance, this:

<cfhttp url="">

becomes this:

cfhttp (url="");

This is discussed under “Script support for tags” on the CFML reference page for CFSCRIPT, which goes on to say how subtags would be specified with braces {}, but that’s beyond the scope of this article.

But what about attributes for CFFUNCTION?

My point here, though, is that if someone were converting a CFFUNCTION in to a script method, they may wonder what to do. Why?

Because a script-based function has long required (as in nearly all languages) that what appears after the method name is a parenthesized list of the method’s actual arguments (if any). So this:

<cffunction  name="test2" returntype="numeric">
<cfargument name="testarg" type="numeric">
<cfreturn testarg * 2>

can be written in script as:

numeric function test2 (numeric testarg) {
return testarg * 2;

But what if you had specified the output=”false” attribute for the function, or perhaps returnformat=”json”? As in:

<cffunction  name="test2" returntype="numeric" output="false" returnformat="json">

Where should those appear, when converted to script? It’s just not obvious, because the parentheses after the method name are already “used” for naming the arguments to the method.

And no, you cannot put these attribute names inside the same parentheses… Well, you can, but they will instead be regarded as literally arguments to the method, and will NOT be processed like the attributes to the CFFUNCTION had been!

The solution, for CFFUNCTION attributes

So as I said at the opening, you provide them AFTER the arguments, such as:

numeric function test2 (numeric testarg) output="false" returnformat="json" {
return testarg * 2;

Now, I’ve said above that you CAN write the function that way in script, but to be clear you could also write it as with everything after the arguments:

function test2 (numeric testarg) returntype="numeric" output="false" returnformat="json" {
return testarg * 2;

Notice how the returntype can be specified EITHER like the other “attributes” (after the method’s arguments), or it can be specified in the function declaration, before the method name. The same is true of the access attribute. Let’s say this was a method in a CFC and we wanted to declare it to be access=”remote”. We could do it either as:

remote numeric function test2 (numeric testarg) output="false" returnformat="json" {
return testarg * 2;

or we could just as well do it all as attributes/arguments, as in:

function test2 (numeric testarg) access="remote" returntype="numeric" output="false" returnformat="json" {
return testarg * 2;

It’s just down to coding styles, really.  (Folks from other languages would probably find the first style more familiar.)

Again the main point of this article has been how to handle those “other” attributes to CFFUNCTION, which are NOT able to be specified that way, but also couldn’t be put “inside the parentheses” as with other CFML tags converted to script. Again they MUST be placed after the method’s arguments.

I hope this was helpful to some readers. It was prompted by a recent discussion in the CF forums, and after offering this answer there I decided to turn it into an article here.

Additional Resources for converting tags to script

Before concluding, I will note that for more on converting CFML to script, there are several resources.

Besides the doc reference I offered above, which was the CFML Reference page for the CFSCRIPT tag itself, there is also more in the Developing ColdFusion Applications manual, such as the page, Using CFScript statements.  Another substantial resource on CFSCRIPT, especially regarding converting from tags to script, is Tony Junkes page full of CFML Tag to Script Conversions.

Perhaps still more compelling is the nifty online tool, free from Pete Freitag, called . With that, you can just type or paste in some CFML on the left side and it will show you the corresponding  script on the right…and the tool does indeed properly handle these “other” attributes for CFFUNCTION! 🙂

I’ll add in closing that some may want to point out that some “attributes” of a CFFUNCTION can also be specified in script by declaring them as annotations within the method definition. That is true of metadata, like hint, description, etc., but I found they did not work (at least in my test in CF2016) for the other attributes to CFFUNCTION. I welcome clarifications or corrections in the comments here.

Add Comment