« Return to Thread: content_for best practices

Re: content_for best practices

by Jacob Atzen-3 :: Rate this Message:

Reply to Author | View in Thread


Josh wrote:

> Thanks for the reply Jacob. That blog post was actually one of the
> first links I came across.
>
> I liked the idea for conditionals like this example:
>
>   <%= (sidebar = yield :sidebar) ? sidebar : render(:partial =>
> 'shared/sidebar') %>
>
> I guess I could do something like so:
>
> view.rhtml:
>   <% content_for :sidebar do %>
>     <div id="sidebar">
>       all of my content in here
>     </div>
> <% end %>
>
> and then in my layout:
>   <%= (sidebar = yield :sidebar) ? sidebar : '' %>
>
> but I'm still having to type that sidebar div everytime.

If you have a default sidebar, then you could just put the div in the
layout, right?

Or you could make it into a helper method, something like (untested):

def conditional_element(name)
   element = yield(name.to_sym)
   content_tag(:div, element, :id => name.to_s) if element
end

I'm not sure exactly what the problem you're trying to solve is, so
maybe I'm misunderstanding something :)

--
Cheers,
- Jacob Atzen


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

 « Return to Thread: content_for best practices