Hey All, I would like some logic that would help me determine whether a yield has any content to it. For example: <div class="submenu"> <%= yield :submenu %> </div> I only want to display the div around the yield if the yield will have content. Any ideas? Thanks, Jim --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 4, 2009, at 7:18 PM, James Englert wrote:> Hey All, > I would like some logic that would help me determine whether a > yield has any content to it. For example: > > <div class="submenu"> > <%= yield :submenu %> > </div> > > I only want to display the div around the yield if the yield will > have content. > > Any ideas?You can do: <% if !yield(:submenu).strip.blank? -%> <div class="submenu"> <%= yield :submenu %> </div> <% end -%> The call may be expensive so you might want to create some other method of determining whether the submenu is likely to have content. But this should work. HTH --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just confirming, its expensive since it renders the yield twice, correct? What if i did something like: <% yield_contents = yield(:submenu) if !yield_contents.strip.blank? -%> <div class="submenu"> <%= yield_contents %> </div> <% end -%> Would that work? I guess I never realized that I could yield twice. Thanks for the tip. Much appreciated. On Sun, Jan 4, 2009 at 10:52 PM, s.ross <cwdinfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Jan 4, 2009, at 7:18 PM, James Englert wrote: > > > Hey All, > > I would like some logic that would help me determine whether a > > yield has any content to it. For example: > > > > <div class="submenu"> > > <%= yield :submenu %> > > </div> > > > > I only want to display the div around the yield if the yield will > > have content. > > > > Any ideas? > > You can do: > > <% if !yield(:submenu).strip.blank? -%> > <div class="submenu"> > <%= yield :submenu %> > </div> > <% end -%> > > The call may be expensive so you might want to create some other > method of determining whether the submenu is likely to have content. > But this should work. > > HTH > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 4, 2009, at 8:47 PM, James Englert wrote:> Just confirming, its expensive since it renders the yield twice, > correct? What if i did something like: > > <% yield_contents = yield(:submenu) > if !yield_contents.strip.blank? -%> > <div class="submenu"> > <%= yield_contents %> > </div> > <% end -%>That should work. A different solution altogether would be to have the submenu contain the div wrapper so you can make the test there and eliminate all the gnarly logic in your layout. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---