[coldbox-4.1.0] Problem understanding renderView

Hi All,

I’m playing around with a simple helloworld app and seeing how I can pass data around and display it.

In my models/helloWorld.cfc I have a query that returns 10 rows.
In my handlers/helloWorld.cfc I have the following:

function index(event,rc,prc){
event.paramValue(“empid”, 0);

prc.Users = getModel(“helloWorld”).read(empid: 0);
renderView(view=‘helloWorld/index’,collection=prc.Users, collectionAs=‘myTest’);
}

In views/helloWorld/index.cfm I have the following:

helloWorld.index

hi
or

The prc dump displays the table as I would expect so I’ve commented it out, but when I try to dump either index (default name for collection based on docs) or myTest I get the following error(s):

Event: helloWorld.index
Routed URL: helloWorld/
Layout: N/A (Module: )
View: N/A
Timestamp: 09/16/2015 02:29:09 PM
Type: Expression
Messages: Variable INDEX is undefined.

or

Event: helloWorld.index
Routed URL: helloWorld/
Layout: Main.cfm (Module: )
View: helloworld/index
Timestamp: 09/16/2015 02:32:53 PM

If you dump the variables scope instead in the view, what do you see?

The code as you’ve showed it would never render any output since renderView() returns the HTML of the view. Typically you would embed that call inside of another view or layout where you wanted the collection to be displayed. Or, set the return value into a variable for later use.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

There is a lot going on there when I dump the variables but I don’t see either a reference to index or mytest.

I see the prc.Users table that I created and the this.prc.Users table.

Here’s the renderView:

RENDERVIEW
function renderView

Arguments:

Name Required Type Default
view Optional Any [empty string]
args Optional struct [runtime expression]
module Optional Any [empty string]
cache Optional boolean false
cacheTimeout Optional Any [empty string]
cacheLastAccessTimeout Optional Any [empty string]
cacheSuffix Optional Any [empty string]
cacheProvider Optional Any template
collection Optional Any
collectionAs Optional Any [empty string]
collectionStartRow Optional numeric 1
collectionMaxRows Optional numeric 0
collectionDelim Optional Any [empty string]
prePostExempt Optional boolean false

ReturnType: Any

Roles:

Access: public

Output:
DisplayName:
Hint: Render out a view
Description:

-Jim

Sorry for reposting my entire first post when it quoted in my last reply.

Brad,

When I put the renderView in my view page (index.cfm) it has timed out every time with the following error:

Here’s the code I put in the index.cfm

#renderView(view=‘helloWorld/index’,collection=prc.Users, collectionAs=‘myTest’)#

The request has exceeded the allowable time limit Tag: cfoutput

-Jim

Are you dumping out the collection? I’m guessing you have a circular reference or very complex object graph that’s bringing down the server. I’ve tried it with a simple array of strings and it works just fine. A tool like Fusion Reactor will tell you what the page is doing before it times out.

Also, there should be no difference between calling it from a view or from a handler. That method is inherited from the FrameworkSupertype, so it’s literally the same code that executes either way.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

You can’t do that without that happening.

When you use RenderView in that manner, it is like calling a template, that will be doing a loop on it every time. For example in something I was working on some time ago.

The collection, can be as the name suggest a collection. But you need to know in the template what sort of collection, is it a struct or an Array. Or even an Entity in your case.

Think of the following code using the render view, as a loop that keeps getting called until all itemns in the collection of being cycled through.

//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
public string function displayForumIndex( template = “displayForumIndex”, collectionAs = “forum”, args = structnew()) {
var prc = getRequestCollection(private = true);
if( structKeyExists(prc,“forums”) ) {
return renderView(view="…/templates/#arguments.template#", collection = prc.forums, collectionAs = arguments.collectionAs, args = arguments.args);
}
throw(message=“Entries not found in collection”, detail = “This probably means you are trying to use the entries in an non-index page”, type = “ContentBox.CBHelper.InvalidEntriesContext”);
}

I think I was recursively calling index which was the cause of the timeout, got it.

I’ve changed the handler helloWorld to just an event.setView.

Inside view/helloWorld/index I’m doing:

`

helloWorld.index

hi
#renderView(view='helloWorld/list',collection=prc.Users, collectionAs='mytest')#

`

and this is causing view/helloWorld/list to show 10 times (YEA!) but…

it’s displaying the same data each time.

Here is my list.cfm in it’s entirety:

`

helloWorld.list

#mytest.fiscalday# #_counter# of #_items#

`

and here is the first couple of “rows” of the result:

helloWorld.index

query
RESULTSET
query
DATE FISCALDAY PERIOD WEEKNUM YEAR
1 2004-12-27 00:00:00.0 1 01 1 2005
2 2004-12-28 00:00:00.0 2 01 1 2005
3 2004-12-29 00:00:00.0 3 01 1 2005
4 2004-12-30 00:00:00.0 4 01 1 2005
5 2004-12-31 00:00:00.0 5 01 1 2005
6 2005-01-01 00:00:00.0 6 01 1 2005
7 2005-01-02 00:00:00.0 7 01 1 2005
8 2005-01-03 00:00:00.0 8 01 2 2005
9 2005-01-04 00:00:00.0 9 01 2 2005
10 2005-01-05 00:00:00.0 10 01 2 2005

CACHED false
EXECUTIONTIME 2
SQL SELECT top 10 * FROM PeriodLookUp
hi

helloWorld.list1 1 of 10

query
RESULTSET
query
DATE FISCALDAY PERIOD WEEKNUM YEAR
1 2004-12-27 00:00:00.0 1 01 1 2005
2 2004-12-28 00:00:00.0 2 01 1 2005
3 2004-12-29 00:00:00.0 3 01 1 2005
4 2004-12-30 00:00:00.0 4 01 1 2005
5 2004-12-31 00:00:00.0 5 01 1 2005
6 2005-01-01 00:00:00.0 6 01 1 2005
7 2005-01-02 00:00:00.0 7 01 1 2005
8 2005-01-03 00:00:00.0 8 01 2 2005
9 2005-01-04 00:00:00.0 9 01 2 2005
10 2005-01-05 00:00:00.0 10 01 2 2005

CACHED false
EXECUTIONTIME 2
SQL SELECT top 10 * FROM PeriodLookUp

helloWorld.list1 1 of 10

query
RESULTSET
query
DATE FISCALDAY PERIOD WEEKNUM YEAR
1 2004-12-27 00:00:00.0 1 01 1 2005
2 2004-12-28 00:00:00.0 2 01 1 2005
3 2004-12-29 00:00:00.0 3 01 1 2005
4 2004-12-30 00:00:00.0 4 01 1 2005
5 2004-12-31 00:00:00.0 5 01 1 2005
6 2005-01-01 00:00:00.0 6 01 1 2005
7 2005-01-02 00:00:00.0 7 01 1 2005
8 2005-01-03 00:00:00.0 8 01 2 2005
9 2005-01-04 00:00:00.0 9 01 2 2005
10 2005-01-05 00:00:00.0 10 01 2 2005

CACHED false
EXECUTIONTIME 2
SQL SELECT top 10 * FROM PeriodLookUp

helloWorld.list1 1 of 10

query
RESULTSET
query
DATE FISCALDAY PERIOD WEEKNUM YEAR
1 2004-12-27 00:00:00.0 1 01 1 2005
2 2004-12-28 00:00:00.0 2 01 1 2005
3 2004-12-29 00:00:00.0 3 01 1 2005
4 2004-12-30 00:00:00.0 4 01 1 2005
5 2004-12-31 00:00:00.0 5 01 1 2005
6 2005-01-01 00:00:00.0 6 01 1 2005
7 2005-01-02 00:00:00.0 7 01 1 2005
8 2005-01-03 00:00:00.0 8 01 2 2005
9 2005-01-04 00:00:00.0 9 01 2 2005
10 2005-01-05 00:00:00.0 10 01 2 2005

CACHED false
EXECUTIONTIME 2
SQL SELECT top 10 * FROM PeriodLookUp

Shouldn’t this be incrementing like a cfloop over a collection would increment?

-Jim

Yeah, not sure. But personally you don’t need the renderView in the main view, there is nothing saying it has be in the controller. But unless you have to have it there, I would stick with it being in the controller.

But it is possible, the view is being cached maybe…

Glancing at the code, I assume you’ll need to reference the query record with the “_counter” variable. So, in your case #myTest.fiscalday[ _counter ]#

It would actually be kind of handy if ColdBox gave you a struct that represented that specific row. Lucee makes this easy, but I assume Luis never did it by default since Adobe CF doesn’t have anything built in for that.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Except _counter is always one.

Jim,

Just out of curiosity if you remove the cfoutput from the template that is being looped, what results do you get. I recall the outer loop getting reset under certain conditions when using a cfoutput in a cfloop. I don’t have an install to test this out with ColdFusion.

Ah, so it is. I didn’t catch that and hadn’t tried it myself yet.

Well, congratulations Jim, you’re apparently the first person to ever use a view collection on a query containing more than one record! This is a bug. The following line in coldbox.system.web.Renderer

variables._counter = arguments.collection.currentRow;

Should instead be:

variables._counter = x;

How do you feel about trying out our pull request process? :slight_smile:

http://blog.coldbox.org/blog/how-to-create-a-jira-account-and-enter-coldbox-tickets
http://blog.coldbox.org/blog/how-to-submit-pull-requests-to-the-coldbox-platform

To be honest though, I’m a little curious about just switching over the behavior to return a struct for each row. I think it’s fair to say we wouldn’t break any existing code.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Struct is also more scalable as well.

I went ahead and created this ticket so we didn’t lose track of it:

https://ortussolutions.atlassian.net/browse/COLDBOX-475

You’re still welcome to help submit a pull request.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks for all of the above suggestions Brad and Andrew.
I manually made the change to the renderer so the _counter is incrementing now.

When I started to referring to the variables as date:#mytest.date[_counter]# I returned the data expected much like using query[currentrow].
From the docs I’d have anticipated a struct being passed but that’s fine.

Andrew - When I removed the cfoutput around the list.cfm it didn’t cause an error but it didn’t resolve the variables either.

I now consider this issue resolved as I now have a better handle on what I was doing wrong and how to use it.

thanks :smiley:
-Jim

Glad to hear it’s working Jim. Please note though, based on the ticket description and comments, we’ll be switching the behavior to use an actual struct in the next version. Just keep that in mind.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Cool no problem, it is a very rare thing I have seen, but I have seen it reset the outter loops variables from time to time. But Brad’s suggestion and fix is the way to go though.