Can variables in the template be used in the layout?


#1

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!


#2

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


#3

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!


#4

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