# Instance variables in layouts

**URL:** <https://www.ruby-forum.com/t/instance-variables-in-layouts/65063>\
**Category:** Rails\
**Created:** [August 15, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/instance-variables-in-layouts/65063 "2006-08-15T23:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 15, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/instance-variables-in-layouts/65063/1 "2006-08-15T23:00:00Z")

</div>

There’s probably a simple answer to this simple problem, but I am trying  
to figure out how to reference an instance variable within a layout. In  
the controller, one method corresponds to a view of the same name. Those  
instance variables in the method can be used in the corresponding view.  
However, I want my instance variable to be accessible throughout all my  
views, not just one, without having to set the instance variable in each  
method. Any solutions? Thanks!

-Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 16, 2006, 5:31am UTC](https://www.ruby-forum.com/t/instance-variables-in-layouts/65063/2 "2006-08-16T05:31:28Z")

</div>

If you have an attribute accessor on your controller, then you can  
simply invoke the attribute accessor to read the controller instance  
variable:

In the controller:

```
 attr_accessor :foo, :bar

```

Or simply

```
 def get_myvar
     return @myvar
 end

```

The controller is an instance variable of your view. You can read the  
controller’s instance variables from inside your view with something  
like

@controller.get\_myvar

or

@controller.foo

I’ve just been playing around with this myself at  
[http://robmela.com/stupidtricks/introspection](http://robmela.com/stupidtricks/introspection)

There was also a useful thread back in June on instance variables in  
controllers.

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 16, 2006, 7:38am UTC](https://www.ruby-forum.com/t/instance-variables-in-layouts/65063/3 "2006-08-16T07:38:35Z")

</div>

Gilles wrote:

> However, I want my instance variable to be accessible throughout all my  
> views, not just one, without having to set the instance variable in each  
> method. Any solutions? Thanks!

Would this work for you?

before\_filter :init

def init  
@myvar = … #whatever you want here  
end

init will run before any action is invoked, so I think it should be  
available in your layout regardless of which action was called.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 16, 2006, 7:48am UTC](https://www.ruby-forum.com/t/instance-variables-in-layouts/65063/4 "2006-08-16T07:48:39Z")

</div>

Jeff C. wrote:

> Gilles wrote:
> 
> > However, I want my instance variable to be accessible throughout all my  
> > views, not just one, without having to set the instance variable in each  
> > method. Any solutions? Thanks!
> 
> Would this work for you?
> 
> before\_filter :init
> 
> def init  
> @myvar = … #whatever you want here  
> end
> 
> init will run before any action is invoked, so I think it should be  
> available in your layout regardless of which action was called.
> 
> Jeff  
> [softiesonrails.com](http://softiesonrails.com)

And if you want all controller to have an instance variable put that  
snippet in application.rb

And layouts works just like views.

\<%= @myvar %\> will work identically in both layouts and views. And  
@myvar can be set in application.rb for all controllers in your whole  
application via the above posters suggestion.
