ColdBox 3.7.0 Layouts and localized css/js

I have an application where a variety of different views share a common layout. The layout includes a header and footer and some menu functionality and it defines the head section of the site where css is included. Each specific view has its own js and css files associated with it. When a page loads, I would like for all of the css to be loaded in the head section and all of the js to be loaded at the bottom of the page, but I do not want to include any css or js files that are not applicable to the view being loaded. Is there a way, using ColdBox, to accomplish this?

I am coming from an ASP.NET MVC background where it is possible to do this in the layout:

My App Title @RenderSection("css")

and this in the view:

@section css
{

}

This way the styles local to a view are defined in the view and merged into the layout when the view is rendered. Is there a way to accomplish something similar using ColdBox? Or is there another way to achieve the same result?

Thanks

No internal method that I’ve found, but what I ended up doing was adding a method to a Request Context Decorator ( http://wiki.coldbox.org/wiki/RequestContextDecorator/wiki/RequestContextDecorator.cfm - don’t forget to configure in your config/Coldbox.cfc)

Example:

`

component entityname=“RequestDecorator” output=“false” extends=“coldbox.system.web.context.RequestContextDecorator” {
public void function configure(){
//set our core defaults for requests
set_defaults();
}

public function addCSS(sheet,media=“all”){
var stylesheet = ‘’&chr(10);
setValue(“stylesheets”,getValue(“stylesheets”)&stylesheet);
}

/**

  • Private methods
    **/

/**

  • Default RC Variables
    **/
    private function set_defaults(){
    setValue(“stylesheets”,"");
    }

}

`

Then I just use a method the controllers to add the sheets:

`

event.addCSS(“css/mystyles.css”);

`

And finally render at the head of my template like so:

`

#event.getValue("stylesheets")# `

You can use the addAsset() method to add css or js to the header. To add things to the footer you will need to do so manually by leveraging variables like JClausen shows or announcing an interception point. I like to have a footer variable if possible. However, I think in ForgeBox there is a plugin for this already.

http://www.coldbox.org/forgebox/view/JavaScript-Defer

signature0.jpg

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano