Using Layout
Layout files for Mack application are stored in app/views/layouts folder. When a Mack application is first generated, it will have a default layout file, application.html.erb. This layout file will be used by all the views defined in the application, unless otherwise specified. This section of the document will discuss how we can control which layout to use in a controller, and also take a peek at the inside of the layout file.
Layout usage is defined in a controller. It can either be set globally (i.e. all action will use that layout), or on a per action basis; Layout can also be turned off if desired.
The following code shows how to set the layout globally:
class MyController include Mack::Controller layout :my_cool_layout endThe following will set the layout per action:
class MyController
include Mack::Controller
def index
render(:text, "I've used a custom layout in the action!", :layout => :my_cool_layout)
end
end
Also note that by setting the layout explicitly in an action, it will overwrite the global layout setting.
class MyController
include Mack::Controller
layout :application
def index
render(:text, "I've changed the layout in the action!", :layout => :my_cool_layout)
end
end
And to disable layout, all you have to do is set layout to false.
class MyController
include Mack::Controller
def index
render(:text, "I've set my layout to false!", :layout => false)
end
end
The most important section of a layout file would be how to get the actual content of the rendered page integrated into the layout. If you examine the content of application.html.erb that got generated by the framework, you will noticed the following section:
... <%= yield_to :view %> ...And that's telling the framework that's the location where you want the rendered page inserted.