ColdFusion Dynamic Argument Naming in Method Calls

I created a custom tag recently that required a method argument name be passed as an attribute. While I’m familiar with the usual techniques of using dynamic variable names, they didn’t work in this situation.

Here’s what I was attempting to do:

Main Page





Custom Tag






The idValue wasn’t an issue but the idColumn wouldn’t work. I tried Evaluate and “#attributes.idColumn#” and a few other similar methods to no avail. Finally, I remembered that you can pass a structure as an argument collection. So I ended up creating a new structure and passing it like this:







And that finally worked.

If there’s another solution out there, please enlighten me.

-rG

7 thoughts on “ColdFusion Dynamic Argument Naming in Method Calls

  1. Hi Ray,

    Always an honor to have a comment from you. I did think of using cfinvoke with arguments but I’m actually using an init method, and I’ve read that createObject is better for server load, and I’m stubborn 🙂 .

  2. Using argument collections can be very helpful sometimes but I have run into issues with it in the past. One thing about cfinvoke though is that it is shared server safe so if your going to be hosting this on older versions of CF where you might not have cfobject enabled you would be better off using cfinvoke.

    As a somewhat related side note when you pass a simple object from flex into CF it translates it as an argumentCollection and you ended up with each property as an individual argument to your method. Incredibly helpful when confronted with the same issues your facing when your properties in flex differ from those in your cf classes and ultimately your db.

    Though ultimately I only use the object class in CF as I never find myself on that shared server environment.

  3. createObject isn’t better. it is just different. It has 0 impact on performance. You can use cfinvoke with init just as easily:

    (pseudo-code)

    cfinvoke component=”somecomponent” method=”init” returnVariable=”myCFC”
    cfif stuff here with dynamic cfinvokecomponent

  4. I’ll second Ray’s comment. Your custom tag could be as follows:

    Keep in mind that neither CreateObject nor cfinvoke is “better”. CreateObject is more succinct, which is easier in most situations. cfinvoke is more verbose, which is helpful in situations like the one you are describing.

  5. hm… my code code scrubbed. Trying again with square brackets:

    [cfinvoke component=”com.mycomponent” method=”getRecord” returnvariable=”qRecord”]
    [cfinvokeargument name=”#attributes.idColumn#” value=”#attributes.idValue#”]
    [/cfinvoke]

  6. Thanks much for the replies. I knew that cfinvoke was an option but I’m actually more comfortable with the creatObject syntax. I probably should have used cfinvoke instead of wasting time trying to figure out how to make createObject work but sometimes when I’m pursuing a solution I can be as stubborn and persistent as, well… a rabid gadfly.

Comments are closed.