[coldbox-3.8.1] renderView keeping old args struct when using cfparam defaults

Hi,

I’m using the args argument of the renderview function to pass in a variable to change the output of a view but I’ve noticed when calling renderview with a view which is using cfparam to provide a default value for a variable in args the same args struct exists for the next call to renderview even though the next call should be getting a new args struct, is this correct behavior or a bug with the renderview function? The code from the views to produce this behavior is below.

views/main/index.cfm

<cfoutput> #renderView(view: "main/view1")# #renderView(view: "main/view2")# </cfoutput>

views/main/view1.cfm

<cfparam name="args.a" default="1"> <cfdump var="#args#" label="view 1 args:">

views/main/view2.cfm
<cfdump var="#args#" label="view 2 args:">

The Result:

This is because the views share the same even request, therefore the variables get set into the same local scope as the event handler. Also there is no need to use cfparam, you can use the prc to store or get variables. I would prefer that over the cfparam option.

For example

<cfset var prc = event.getCollection(private = true);

if inside a handler, there is no need to the above. But the following is what you would need.

//********************************************

//
//********************************************

public void function index(event, rc, prc) {
prc.getValue(name = ‘myVariable’, defaultValue = ‘’, private = true);
writeDump(prc); abort;
}

If you where to run this code, there will be a variable named myVariable sitting in the prc scope.

Hi Andrew

I forgot to mention in my post that if i do supply a value for args.a when calling renderView the args struct doesn’t appear to be shared between calls which makes me think that it not suppose to share the same local scope.


As an example if i change views/main/index.cfm to

<cfoutput> #renderView(view: "main/view1", args:{a=1})# #renderView(view: "main/view2")# </cfoutput>

The result (shown below) is that they don’t appear to be sharing the same args struct. I would use prc but the view I’m using is being called multiple times in the page and the variables i need to pass it change for some of the renderview calls.

Yes, that is expected behavior.

ColdFusion treats methods like this with a set of new args each and every time you call a function.

The only way is to pass them in via the second arg, or use the Request Context or the private version.

//********************************************

//
//********************************************

public void function index(event, rc, prc) {
prc.getValue(name = ‘myVariable’, defaultValue = ‘’, private = true);
writeDump(prc); abort;
}

or just pass it in again like you did the first time.

The RC and PRC exist for the entire request, you can think of this like the request scope only you get a private one as well as a public scope.