What is the best practice for setting and passing variables to layout
pages? I previously used cfmodule for layouts and passed attributes to
the layout page in the tag. Ex:
<cfmodule template="/_subpagelayout.cfm" title="How to Receive Jesus
Christ into Your Life" section="resources"
headbanner="salvation.jpg"...>
I then decided what side menu and/or pod to show based on a passed
attribute (<cfmodule..section="television"..>)
Do I need to create a request scoped struct in each event handler with
values for these attributes right before setting the view? What about
default values for attributes that don't change much, where would they
go?
Thanks in advance for the help!
--Marcus Cox
Hi Marcus,
All of the things you want to do are already available to you by Coldbox via the Request Collection. The request collection object is available in any layout/view/plugin/handler/interceptor etc. You can set/get values from it without passing it around. I recommend you read the request context guide and the event handlers guide in the wiki. It will clarify what the request collections is for.
In summary, the problem of passing data around has been solved by the framework, by providing you with an object that is available anywhere within the framework. A cool feature also is that layouts and views both receive a scope called “rc”. This scope is a direct reference to the request collection, so you can get/set values from it like any other normal coldfusion structure:
rc.name = “Luis”
My name is #rc.name#
Thanks for the response Luis. I already read the docs and understand
the request collection. I guess I was looking for best practices here.
I am already doing this in each one of my view handlers:
<cffunction name="dspSalvation" ...>
<cfargument name="Event" ...>
<cfset var rc = event.getCollection()>
<!--- View/Layout Page Settings --->
<cfscript>
rc.Title = "How to Receive Jesus Christ into Your Life- Benny Hinn
Ministries";
rc.Section = "resources";
rc.AdditionalCSS = "";
rc.ExpandMenuClassName = "";
rc.HeadBannerImage = "salvation.jpg";
rc.BodyOnLoadFunction = "";
rc.BodyOnUnloadFunction = "";
</cfscript>
<cfset Event.setView("salvation")>
</cffunction>
Is this the preferred way to do this for the Layout page to act upon?
Is there a way to set those up with default values so I only have to
pass the ones that change?
Thanks for the help?
Well, there are several ways to do that. You can set global wide rc variables in the on request start handler, and then your specific events can override as needed. Or you can create an event for it, or even an interceptor that sets up all your default layout variables on “preRender” or “preProcess”.
In my experience, I have loaded up some global options, in my request start handler.
Luis
Ok..So I can basically drop the <cfscript> code that's currently in my
display event handler into the main.onRequestStart event handler and
override as needed? I will try that..For some reason I was thinking
you should add something (maybe in YourSettings sections) to the
coldbox.xml.cfm file.
Thanks!
BTW..It was nice to meet you at CFObj().
You too marcus!!
Let me know if you need anything else.