# Can variables in the template be used in the layout?

**URL:** <https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495>\
**Category:** Rails\
**Created:** [March 7, 2006, 1:41am UTC](https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495 "2006-03-07T01:41:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_heath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ryan\_heath](https://www.ruby-forum.com/u/ryan_heath)\
**Post date:** [March 7, 2006, 1:41am UTC](https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495/1 "2006-03-07T01:41:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ryan\_heath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ryan\_heath](https://www.ruby-forum.com/u/ryan_heath)\
**Post date:** [March 7, 2006, 3:41am UTC](https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495/2 "2006-03-07T03:41:51Z")

</div>

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 || 'Company Reports' %\>

(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

---

<div class="post-metadata">

**Author:** ![ryan\_heath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ryan\_heath](https://www.ruby-forum.com/u/ryan_heath)\
**Post date:** [March 7, 2006, 4:25am UTC](https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495/3 "2006-03-07T04:25:59Z")

</div>

Justin F. 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 || 'Company Reports' %\>
> 
> (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 = "Main" %\>

#layout

\<% if @title == “Main” %\>  
Back  
\<% 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!

---

<div class="post-metadata">

**Author:** ![ryan\_heath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ryan\_heath](https://www.ruby-forum.com/u/ryan_heath)\
**Post date:** [March 7, 2006, 4:42am UTC](https://www.ruby-forum.com/t/can-variables-in-the-template-be-used-in-the-layout/54495/4 "2006-03-07T04:42:12Z")

</div>

ryan heath wrote:

> > > within that? Or can they be accessed all throughout the page?  
> > > nil)
> 
> \<%= @title = "Main" %\>
> 
> #layout
> 
> \<% if @title == “Main” %\>  
> Back  
> \<% end %\>
> 
> Something like that will work?

Well, yes, except that you would always have tags  
in your layout, rather than the template, because the   
has the be in the . 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
