In this sixth part to a multi-part series on the Action Pack library for Rails, you'll learn how to create a template and work with layouts. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Ruby on Rails Templates and Layouts - Working with Ruby-on-Rails Layouts (Page 2 of 2 )
Rails uses layouts to interpolate the output of an individual template into a larger whole—a reversal of the common pattern of including a shared header and footer on every page (which if you’ve done any work in other languages like PHP or ASP will be all too familiar). The scaffold generator we ran in Chapter 3 created a layout file and placed it in app/views/layouts/events.rhtml. It’s named after the controller we generated (events), and will be applied automatically to that controller only. That’s the way it works in Rails. Just as an action tries to render itself using a view that matches its name, so does a controller attempt to use a layout that matches its name. However, we’d like our layout to apply to all our controllers, including the one we’re working on now.
To have it apply to all our controllers, we can rename it toapplication.rhtml. Go ahead and do that now, and when you’re finished, open it in your editor. You should see something like the file shown in Listing 6-3.
Listing 6-3. The app/views/layouts/application.rhtml File
At rendering time, the layout will yield the results of the template fragment’s execution in place. See the<%= yield %>bit that we’ve highlighted in bold? That’s the important part. Wherever you put theyield keyword is where your content will go.
One more thing to note here: Rails is all about convention over configuration. Here, the convention is that a layout with the nameapplication.rhtmlis automatically applied to all templates unless an alternate is specified in the controller. This means that if you were to change the name of the layout as it stands, it wouldn’t be automatically applied. If you wanted to apply a different layout to a given controller, you could specify it in the controller itself using the class method,layout.
class ExampleController < ApplicationController layout 'my_layout' # Will look for a layout in app/views/layouts/my_layout.rhtml end
COMMON LAYOUT CONVENTIONS
A few conventions apply to working with layouts:
A layout named application.rhtml will be applied automatically unless a more specific candidate exists or is explicitly specified in the controller.
A layout that matches the name of a controller will be automatically applied if present. Controller-specific layouts take precedence over the application-level layout.
You can use the layout directive at the class level in any controller (that is, not inside an action) to set the layout for the entire controller: layout 'my_layout'.
You can include a layout for a specific action with an explicit call to render inside the action: render :layout => 'my_layout'.
Sometimes you want to render an action without a layout. In that case, you can pass false in place of the layout name: render :layout => false.
In practice, we usually just use application.rhtml and rarely take advantage of the controllerspecific layout functionality. On the occasions when we need to use a different layout for a particular controller, we use the layout directive.
Please check back for the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.