[Coldbox 3.6.0] Question on getting data for none view related items in a layout.

My understanding/thinking is the Layout is kind of the master structure of your site.

Views are what you are trying to do at a particular time on a site.

So lets say you are an admin looking at a list of users. So your event might be “users.listusers”

Now when you go to that url, Coldbox loads the “users” handler, and then looks for the “listusers” function. Where it will perhaps call something to load a list of users into a query object.
Then assume listusers sets the view to “users” which might display a table based on the query from “listusers”

That table is output into the layout via the renderView() function.

So far so good I think.

So lets say I have a menu bar at the top of the website which changes depending on who is logged in. In order to get that the menu needs to get a list of items from a menu object to know what to display.

Where does this call to get the menu items fit into the Coldbox workflow?

For example what if it was called from the layout like this renderView(“menu/mainmenu”);

From what I can tell this will not invoke a handler for menu.
I could include the menu call in “listusers” but it seems strange that it would need to be aware of the menu and it’s needs. Not to mention having to put the menu on every handler that “might” use it.
I could include the menu call in onrequest start but now it’s going to be included everytime regardless of wether or not the layout is setup to display it.

It seems to me that the layout which has the render call should somehow be responsible for gathering/invoking the data as well.

There are a variety of ways to solve this problem, but perhaps the most powerful (common?) would be through an interceptor. In a nutshell, you write an interceptor that checks the section of the site that was requested, the user’s credentials, etc., and then adds the correct menu-related data to the rc. Then your layout calls renderView( ‘menu’ ) like normal.

HTH

The part I am having trouble with is that most solutions seem to require the menu to be loaded all the time.

Lets say that the admin next goes to a financial report that is displayed full screen, so we use a different layout that does not include a menu. How does an interceptor know that the menu was not included this time.

I kind of think of the different areas of the layout as widgets and each widget should be responsible for getting what it needs.