Sequenzia
Sequenzia

Reputation: 2381

Variable name in the method of ColdFusion Object

I am trying to set a variable in a cffunction.

The result is this:

<cfset local.layouts.appLayout = '../../app/layouts' & local.appController.new()>

The above code works. In the local.layouts.appLayout structure it assigns the return of the new method in the appControler. That is what I need it to do.

My problem is that I need to dynamically assign the method portion of that statement. I have another variable coreRoute.action that equals "new" in that function but I cannot seem to get the syntax right.

I have tried this:

<cfset local.layouts.appLayout = '../../app/layouts' & local.appController.coreRoute.action()>

That does not work and I can see why. I have also tried this:

<cfset local.layouts.appLayout = '../../app/layouts' & local.appController & #coreRoute.action# & '()'>

I have tried many variations of this syntax and I just cannot get it right.

Anyone have any ideas about how to do this. I am stuck.

Thanks in advance for any help.

UPDATE: With Todd Sharp's help I ended up using this and it worked great:

<cfinvoke component="#local.appController#" method="#coreRoute.action#" returnvariable="local.act">

            <cfset local.layouts.appLayout = '../../app/layouts' & local.act>

Upvotes: 3

Views: 400

Answers (2)

Adam Tuttle
Adam Tuttle

Reputation: 19834

In addition, if you want to do it entirely in script, you can, using this approach:

dynFn = this["foo" & bar];
dynFn(stuff);

This is in a cfc, if you're doing it from outside the cfc or not using a cfc at all, just change "this" to wherever your method is.

Upvotes: 2

Todd Sharp
Todd Sharp

Reputation: 3355

You should look into using <cfinvoke> for dynamic method invocation. Try a Google search for "coldfusion dynamic method invocation" - here's one of the top results:

http://www.bennadel.com/blog/1320-ColdFusion-CFInvoke-Eliminates-The-Need-For-Evaluate-When-Dynamically-Executing-User-Defined-Functions.htm

Upvotes: 7

Related Questions