[coldbox:4054] Conditional layout best practice


I'm porting a legacy Fusebox app to ColdBox 3 M4 so that I can show my
team that maintenance and enhancements much easier using ColdBox. The
layout file has code like this in it at the moment so that banners etc
can be shown on certain pages.

<cfif ListFind( "News,Events", myFusebox.OriginalCircuit )>
<!--- show a banner ad for pages in the news and events fusebox
circuits --->
<!--- for all other fusebox circuits show this content --->

There are several ways I could replicate this, but the best ones seem
to be:

- setting a flag in the main handler which is overridden in the
prehandler for the "News" and "Event" handlers
- using ListFind( "News,Events", ListFirst( Event.getCurrentEvent(),
"." ) ) in the view

The other developers who will maintain this site are designers so I'm
trying to make it easy for them (so that I can get them using

Any other ideas or preferences you can suggest would be great.


- John

It's simpler than that!

Since each 'circuit' will map to a separate event handler (and each
fuseaction will map to a method), just set the desired layout in the
preHandler() for the news.cfc and events.cfc handlers - leaving the
site default in coldbox.xml.


Thanks for the reply Sean :slight_smile:

I don’t want to overrider the whole layout as it is only say the left column content of the main layout that will change, so I don’t want to duplicate layout HTML code. The best solution I’ve found is to use Content Variable Views (called from the preHandler) to get the left column content that changes http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbLayoutsViewsGuide#ContentVariableViews but the docs say “Important: I do not encourage the overuse of content variables as I see them as performance bottlenecks. As best practice, please use them sparingly.”

So I figured I’d maybe use conditional code in the layout, but I’m not that comfortable with that.

  • John

Ah, this is exactly the situation on our project. What we do is set
the view *name* in a variable in RC in the handler and call
renderView( rc.columnView ) in the layout:

function handler( event ) {
    if ( someCondition ) {
        rc.columnView = 'col/one';
    } else {
        rc.columnView = 'col/two';

In the layout:

#renderView( rc.columnView )#

In fact, we even conditionally switch CSS so that if columnView is not
defined in rc, we don't even have the container div for the column.

Ah, that makes a lot sense. Thanks Sean!