Basicly, I have a standard layout with a main content section (the <%= yeild %>), but I also have a sidebar. Previously I was just using <% render :partial => ''path/to/partial'' %>. Now I want the content in the sidebar to be dynamic, should I be using helpers, or can I call another controller and method from inside the rhtml? thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The way that I do this is with content_for. You can use content_for_layout in your main layouts to display individual view content (this is an alternate to yield - I think yield is deprecated?). In the same way you can put content_for_XXX in your layout and have that replaced as well. An example might be the easiest: In your layout: < if !@content_for_sidebar.blank? %> <%= @content_for_sidebar %> <% end %> In your individual views: <% content_for ''sidebar'' do -%> <p>Put some content here!</p> <% end %> Note that you don''t need the if statement in the layout if you always fill out the content_for_sidebar but in my views I often leave some content sections empty. Jon On 10/13/06, Jonathon Marshall <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Basicly, I have a standard layout with a main content section (the <%> yeild %>), but I also have a sidebar. Previously I was just using <% > render :partial => ''path/to/partial'' %>. Now I want the content in the > sidebar to be dynamic, should I be using helpers, or can I call another > controller and method from inside the rhtml? > > thanks > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Jon Walker joninsb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/13/06, Jon Walker <joninsb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The way that I do this is with content_for. You can use > content_for_layout in your main layouts to display individual view > content (this is an alternate to yield - I think yield is > deprecated?). In the same way you can put content_for_XXX in your > layout and have that replaced as well. An example might be the > easiest: > > In your layout: > > < if !@content_for_sidebar.blank? %> > <%= @content_for_sidebar %> > <% end %>It is better to use <%= yield ''sidebar'' %> instead.of <%@content_for_sidebar %>. In your individual views:> > <% content_for ''sidebar'' do -%> > <p>Put some content here!</p> > <% end %> > > Note that you don''t need the if statement in the layout if you always > fill out the content_for_sidebar but in my views I often leave some > content sections empty.If you just need to prepare data for your sidebar, you can use combination of common partial + before_filter in e.g. ApplicationController to prepare data for partial. But if your sidebar is dependant on particular section, I would suggest using nested_layouts plugin (http://nested-layouts.rubyforge.org) and "content_for" helper (described above). So for every "section" (a group of pages with the same sidebar) you could use the same inner layout that would prepare sidebar content and wrap page content in outer (your main) layout. Take a look at nested_layouts plugin documentation - it has example of just what (I guess) you need. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jonathon Marshall
2006-Oct-13 20:36 UTC
Re: Dynamic content outside of <%= yeild %> in layout
Jon, I thought content_for was depricated. Maxim, I think the before_filter and common partial is what Im looking for. I can use the before_filter to load up relavent data and use some basic logic in the rhtml to flip different sidebar ''modules'' on and off. I had tried render_component, but heard that was a wasteful option, not to mention it crashed WEBrick on more than one occasion. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jonathon, You are right. From the rails docs: NOTE: Beware that content_for is ignored in caches. So you shouldn''t use it for elements that are going to be fragment cached. The deprecated way of accessing a content_for block was to use a instance variable named @@content_for_#{name_of_the_content_block}@. So <%= content_for(''footer'') %> would be avaiable as <%@content_for_footer %>. The preferred notation now is <%= yield :footer %>. It is always good when I learn something by answering a question! Jon On 10/13/06, Jonathon Marshall <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Jon, I thought content_for was depricated. > > Maxim, I think the before_filter and common partial is what Im looking > for. I can use the before_filter to load up relavent data and use some > basic logic in the rhtml to flip different sidebar ''modules'' on and off. > > I had tried render_component, but heard that was a wasteful option, not > to mention it crashed WEBrick on more than one occasion. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I thought content_for was depricated.Not exactly. Accessing @content_for_(...) is deprecated (see Jon W.''s post), but the content_for() method itself is not deprecated. Or at least that''s how I read the docs. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---