Forum: Ruby on Rails Can variables in the template be used in the layout?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1e2dd87a31c5d3e07adc9efa375f4dc4?d=identicon&s=25 ryan heath (Guest)
on 2006-03-07 01:41
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!
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-07 03:41
(Received via mailing list)
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
1e2dd87a31c5d3e07adc9efa375f4dc4?d=identicon&s=25 ryan heath (Guest)
on 2006-03-07 04:25
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?  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!
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-07 04:42
(Received via mailing list)
ryan heath wrote:
>>> within that?  Or can they be accessed all throughout the page?
>> nil)
> <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
This topic is locked and can not be replied to.