RSS
20 Oct 2006

Best Practices of Sidebars & Layouts in Ruby on Rails

Author: ceefour | Filed under: Beginner, Rails, Ruby, Web 2.0

This surprises even me, but I’m not sure what is the best way to provide multiple templates, sidebars, etc. (i.e. typical layout/templating stuff) before I read this. Formerly I exclusively use partials, now I know there’s another way, the content_for and yield. It’s not something that all beginner Railers know (including me) so I thought this’d be a great & very useful read.

Via err.the_blog :

We all know Rails compiles views before it compiles the layout. That is, if you set @page_title in your view, you may reference it in your layout. Something like <title>My Site :: <%= @page_title %></title>. content_for is no different: it allows you to ‘assign’ a chunk of HTML or Ruby to a symbol which may be referenced in your layout.

Here’s Larry’s layout:

<html>
<head><title>Larry's Personal Home Page</title></head>
<body>
  <table width="100%">
    <tr><td>
      <%= yield %>
    </td><td>
      <%= yield :sidebar %>
    </td></tr>
    <tr><td colspan="2" align="center">
      Copyright (c) 2006 Larry
    </td></tr>
  </table>
</body>
</html>

The first yield, clearly, will be replaced with Larry’s view when rendered. The second yield is a bit different: he’s passing it a symbol, saying he wants to yield :sidebar at that point in time-whatever :sidebar may be.

Let’s look at Larry’s view:

<font color="red">Welcome to my website.</font><br>
<img src="under_construction.gif"><br>
<font color="blue">It was coded by hand in notepad.</font><br>
<font color="green">Tell me what you <blink>think!!</blink></font><br>

<% content_for :sidebar do %>
  Hot Links<br>
  <a href="http://www.netscape.com">Netscape</a><br>
  <a href="http://www.lycos.com">Lycos</a><br>
  <a href="http://www.walmart.com">Wal Mart</a><br>
<% end %>

You got it, right? yield :sidebar is going to be replaced by whatever block is passed to content_for :sidebar. In this case, Larry’s sidebar.

Read more on Content for Whom?


Technorati :
Del.icio.us :
Ice Rocket :
Flickr :
Zooomr :
Buzznet :
Riya :
43 Things :

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  • snow

    hi, your site is really informative, though it’s only links to other site. Really helpful to me !!!

  • http://sillywebsite.com Boney

    Way to go. Rip off Err the Blog’s content! Why not write something yourself?

  • http://hendy.gauldong.net/ Hendy Irawan

    Dear Boney,

    That’s the whole point of this post. I only quoted a bit of Err’s article, and add a little of my own comments.

    Otherwise, I’d just plainly link to the article, like Google or del.icio.us does, which won’t be as much entertaining nor informative.

    Note that I don’t claim authorship of the entire text. I clearly indicate that it’s Err’s article. And when I said “I thought this’d be a great & very useful read,” I was referring to Err’s post, not mine.

    Hope this clears up some confusion.