Posted by ryan
at 1:21 PM
on Thursday, August 16, 2007
For those of you that weren’t able to attend the very well put together and cozy Ruby Hoedown, it appears that some rough cuts of the talks are making their way up on the ConFreaks site.
The talks covered all sorts of topics and each had different focuses and presenter styles. I thought Marcel’s finale keynote was especially enjoyable if you’re the type that likes to stop your code-slinging every once and awhile to gain new perspectives.
For a good rundown of all the talks, see my fellow werewolf Lyle’s summaries of day 1 and day 2.
tags: ruby,
rubyhoedown
Posted by ryan
at 8:48 PM
on Thursday, August 02, 2007
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