Softies on Rails » Web User Controls vs. Layouts

The scenario: you’re a disciplined web developer, well versed in agile software development and that sorta thing. In an effort to keep things DRY
(and make your own life easier), you want to separate out common sections of your web site into nicely
encapsulated little pieces. For example, a header, footer, or shopping cart that appears on every page. As a simple example, let’s say you want to display a header on every page that says “hello” to the currently logged-on user. (Note: the code that actually gets the name of the currently logged-on user is a discussion for another day.)

In ASP.NET? You’d use a web user control, of course. Maybe, you have something like this: (Granted, Visual Studio does most of this for you.)


Hello <asp:Label id="name" runat="server"/>!

And then, in the code-behind (say, HelloControl.ascx.cs) for the control (or inline, if you prefer):

protected System.Web.UI.WebControls.Label name;

private void Page_Load(object sender, System.EventArgs e)
	name.Text = currentUser.Name;

Then, in the page where you want to use it:

<%@ Register TagPrefix="uc1" TagName="HelloControl" Src="HelloControl.ascx" %>

Then, somewhere in the body…

<uc1:HelloControl id="HelloControl1" runat="server" />

In Rails, when you want to accomplish something similar, you can use layouts. For example, to accomplish our goal in Rails, we could simply create a layout, like this:

	Hello, <%= %>!
	<%= @content_for_layout %>

The @content_for_layout instance variable represents the content generated by the view that uses the layout. If I were to put the above code into a file called application.rhtml, it would used by all views. Very clean and simple… and very little code.

We can also do a host of other cool stuff – nest layouts for more complex needs, override layouts for individual pages, pass data to layouts, among other things. We can use partials for times when we need bits of view code on multiple pages. Or, components if we want to reuse both controller and view code among pages. This is a just one example of how Rails can be as simple or as complex as you want it to be.