Adding A Layout
Last updated
Was this helpful?
Last updated
Was this helpful?
Every time the framework renders a view, it will try to leverage the default layout located in layouts/Main.cfm
by convention. This is a reusable CFML template that gives format to your HTML output and contains the location of where the view you want should be rendered.
Tip : The request context can also be used to choose a different layout at runtime via the event.setLayout()
method or the layout
argument in the event.setView( layout: "login" )
method.
Tip : The request context can also be used to render a view with NO layout at all via the event.noLayout()
method or event.setView( noLayout: true )
The layout has everything you want to wrap views or other layouts with. You can use our rendering methods to do inline renderings or tell ColdBox where the set view should render:
view()
- Render the set view via event.setView()
view( name: "toolbar" )
- Render a
view( "partials/footer" )
- Render a explicit view
layout( name )
- Render another layout within this layout
The call to the view()
method with no arguments tells the framework to render the view that was set using event.setView()
. This is called a rendering region. You can use as many rendering regions within layouts or views.
Let's create a new simple layout with two rendering regions. Open up CommandBox and issue the following commands:
Open the layouts/Funky.cfm
layout and let's modify it a bit by adding the footer view as a rendering region.
Now let's do our footer:
Now, let's open the handler we created before, called handlers/hello.cfc
and add some code to use our new layout explicitly by adding a layout
argument to our setView()
call.
Go execute the event now: http://localhost:{port}/hello/index
and you will see the view rendered with the words funky layout
and footer view
at the bottom. Eureka, you have now created a layout.
As you can see from the footer, we introduced a new function called getSetting()
. All layouts, handlers, interceptors, and views inherit the functionality. There are tons of methods inherited from this class that you can use in your application, from getting models to settings, relocating, async computations, and so much more.