The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: Partials Get Layouts

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: Partials Get Layouts Posted: Aug 2, 2007 6:47 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: Partials Get Layouts
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

If you’ve ever taken the time to factor out the various commonalities that often exist in your Rails views into partials, you’ve probably noticed that there are still opportunities for DRYing up those partials. Well now you have a tool in your toolbox to help partials become first class citizens in the view-world: partial layouts. It’s as easy as adding a :layout option to your render call:

1
2
3
4
5
6
7
8
9
10
<!-- in posts/show.html.erb -->
<%= render :partial => 'header', :layout => 'boxed', :locals => {:post => @post} %>

<!-- in posts/_boxed.html.erb -->
<div class='box'>
  <div id='post_header_<%= post.id %>'><%= yield %></div>
</div>

<!-- in posts/_header.html.erb -->
<%= post.title %> published on <%= post.published_at %>

Notice that the partial layout boxed still uses the partial naming convention of an _ prefix and that it exists in the same directory as the calling file (i.e. it doesn’t have it’s own layouts directory like the other master layouts do). Also notices that any locals passed into the partial render are also made available to the partial layout (post in this case.) You include the content of the partial into the layout by calling yield, as you do in the master templates.

So with the above setup, the rendering of posts/show.html.erb will result in this:

1
2
3
<div class='box'>
  <div id='post_header_1'>Post Title published on August 3rd, 2007</div>
</div>

You also have the ability to render a block of code within a partial layout if you want the consistency a layout provides but don’t necessarily have the need to break off the content into its own partial:

1
2
3
4
<!-- in posts/show.html.erb -->
<% render(:layout => 'boxed', :locals => {:post => @post}) do %>
  <%= post.title %> published on <%= post.published_at %>
<% end %>

DRY away.

tags: ruby, rubyonrails

Read: What's New in Edge Rails: Partials Get Layouts

Topic: Vacation! Previous Topic   Next Topic Topic: ar_mailer version 1.3.1 has been released!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use