Sure thing. You can render as many views as you want inside of other views or inside of your layout. A common work for that is a viewlet. It gives you a nice way to encapsulate little chunks of HTML and include them on any page you want.
For instance, in my main layout, I render a view for each dropdown menu in my site.
Now, the next issue you’ll run into is when your viewlets need data and you want an easy way to wrap up the handler code to get that data with the view it goes with. Otherwise if handler A renders view 1, which in turn renders views 2, 3, and 4, you’ll end up pulling all the data you need into the request collection up in handler A which ties it to all the views.
The easy solution for us is to wrap each view in an event (perhaps I could call them eventlets Instead of doing event.setView() in that event, I just return the output of the rendered view like so:
// Here is a handler
layoutAssets.cfc
component {
property name=“myService” inject;
function HTMLPod1(event, rc, prc) {
rc.data = myService.getData();
// This view will access rc.data and do stuff with it.
return renderView(“layoutAssets/HTMLPod1”);
}
}
Then, in my layout (or another view), just run this event and output its return directly on the page:
#runEvent(“layoutAssets.HTMLPod1”)#
Now you have an encapsulated event that is capable of getting its own data and returning the HTML of the view that it renders. The only thing to do now is to clean up the rc scope so you aren’t dumping stuff in there. For that, use the args parameter to pass your data directly to that view without cluttering up rc like so:
var data = myService.getData();
// This view will now access args.data instead of rc.data.
return renderView(view=“layoutAssets/HTMLPod1”, args=data);
Nice and clean.
Also note, when renderView() is called with no arguments inside a layout, that renders the view set in the handler with setView(). If you pass a view name into renderView, it renders the specific view you’re asking for.
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com