Best Practices of Sidebars & Layouts in Ruby on Rails
Author: ceefour | Filed under: Beginner, Rails, Ruby, Web 2.0This 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 : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Del.icio.us : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Ice Rocket : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Flickr : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Zooomr : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Buzznet : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
Riya : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
43 Things : rubyonrails rails ruby on rails best practices sidebars layouts templates content_for sidebar layout template erb actionview
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.