ryan heath
2006-Mar-07 00:41 UTC
[Rails] Can variables in the template be used in the layout?
This may be a silly question, but I''m wanting to develop a query to pull ''related articles'' based on tags. When a user clicks on an article to read, on that layout, I want a side menu to have the related entries. If I develop the query to do this, can I access the current tags for the entry in the layout? I guess I''m asking, does <% content_for_layout %> keep the variables within that? Or can they be accessed all throughout the page? Thanks! -- Posted via http://www.ruby-forum.com/.
Justin Forder
2006-Mar-07 02:39 UTC
[Rails] Can variables in the template be used in the layout?
ryan heath wrote:> This may be a silly question, but I''m wanting to develop a query to pull > ''related articles'' based on tags. > > When a user clicks on an article to read, on that layout, I want a side > menu to have the related entries. If I develop the query to do this, > can I access the current tags for the entry in the layout? > > I guess I''m asking, does <% content_for_layout %> keep the variables > within that? Or can they be accessed all throughout the page?If you assign to instance variables in your content, you can access those in the layout. For example, a content page might contain <% @title = "Annual Results" %> and the layout might contain <title><%= @title || ''Company Reports'' %></title> (Here I''ve given a default title ''Company Reports'' in case @title is nil) You should be able to do something similar to pass the items for your side menu from the content to the layout containing the menu. Justin
ryan heath
2006-Mar-07 03:26 UTC
[Rails] Re: Can variables in the template be used in the layout?
Justin Forder wrote:> ryan heath wrote: >> This may be a silly question, but I''m wanting to develop a query to pull >> ''related articles'' based on tags. >> >> When a user clicks on an article to read, on that layout, I want a side >> menu to have the related entries. If I develop the query to do this, >> can I access the current tags for the entry in the layout? >> >> I guess I''m asking, does <% content_for_layout %> keep the variables >> within that? Or can they be accessed all throughout the page? > > If you assign to instance variables in your content, you can access > those in the layout. For example, a content page might contain > > <% @title = "Annual Results" %> > > and the layout might contain > > <title><%= @title || ''Company Reports'' %></title> > > (Here I''ve given a default title ''Company Reports'' in case @title is > nil) > > You should be able to do something similar to pass the items for your > side menu from the content to the layout containing the menu. > > JustinOk, seems like it will work. Thanks. Just to clarify: #template <title><%= @title = "Main" %></title> #layout <% if @title == "Main" %> <a href="#">Back</a> <% end %> Something like that will work? I''m still confused how I would involve the tags listed in the template in my query to pull related entries in the layout. Any thoughts on that? Thanks! -- Posted via http://www.ruby-forum.com/.
Justin Forder
2006-Mar-07 03:40 UTC
[Rails] Re: Can variables in the template be used in the layout?
ryan heath wrote:> Justin Forder wrote: >> ryan heath wrote: >>> This may be a silly question, but I''m wanting to develop a query to pull >>> ''related articles'' based on tags. >>> >>> When a user clicks on an article to read, on that layout, I want a side >>> menu to have the related entries. If I develop the query to do this, >>> can I access the current tags for the entry in the layout? >>> >>> I guess I''m asking, does <% content_for_layout %> keep the variables >>> within that? Or can they be accessed all throughout the page? >> If you assign to instance variables in your content, you can access >> those in the layout. For example, a content page might contain >> >> <% @title = "Annual Results" %> >> >> and the layout might contain >> >> <title><%= @title || ''Company Reports'' %></title> >> >> (Here I''ve given a default title ''Company Reports'' in case @title is >> nil) >> >> You should be able to do something similar to pass the items for your >> side menu from the content to the layout containing the menu. >> >> Justin > > Ok, seems like it will work. Thanks. Just to clarify: > > #template > > <title><%= @title = "Main" %></title> > > #layout > > <% if @title == "Main" %> > <a href="#">Back</a> > <% end %> > > Something like that will work?Well, yes, except that you would always have <title> tags in your layout, rather than the template, because the <title> has the be in the <head>. Take a look at my example again. My @title was set in the template (which I was referring to as the content page), and used in the layout.> I''m still confused how I would involve > the tags listed in the template in my query to pull related entries in > the layout. > > Any thoughts on that?Rather than thinking in terms of what gets passed from content to layout, why don''t you just set an instance variable (e.g. @menu_items) in your controller, and pick that up in the layout?> > Thanks! >(going off-line now - it''s 03:40 here!) Justin