# Variables in layouts

**URL:** <https://www.ruby-forum.com/t/variables-in-layouts/51618>\
**Category:** Rails\
**Created:** [January 4, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618 "2006-01-04T17:21:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/1 "2006-01-04T17:21:43Z")

</div>

I have a layout that provides the basic outline of each page in my app.  
It has a title, footer and a navigation bar.

In the title I want to put the name of the current user that is logged  
in.

I could do something like @session[:username] but I would rather  
encapsulate that into a single variable so that I can change the key in  
the session map if I want without having to goto each page that uses it.

It seems that a better way would be to use an instance variable like I  
have seen @page\_title a lot. I dont want to set this in each and every  
action so is there a way that I can set it in the super class  
controller?

Maybe in after\_filter? What do you guys do?

Thanks

Gareth

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/2 "2006-01-04T17:27:51Z")

</div>

> It seems that a better way would be to use an instance variable like I  
> have seen @page\_title a lot. I dont want to set this in each and every  
> action so is there a way that I can set it in the super class  
> controller?

In ApplicationController ?

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/3 "2006-01-04T18:09:35Z")

</div>

Lieven De Keyzer wrote:

> > It seems that a better way would be to use an instance variable like I  
> > have seen @page\_title a lot. I dont want to set this in each and every  
> > action so is there a way that I can set it in the super class  
> > controller?
> 
> In ApplicationController ?

I dont understand what you are asking. I have a superclass for each  
controller that has the before\_filter in it that checks to see if the  
users is logged in and if not redirects to the login page.

This feels like the right place to set some of those intstance variables  
e.g. @username. I was interested in others have solved this problem.

Gareth

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/4 "2006-01-04T18:13:13Z")

</div>

On 1/4/06, Gareth R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the title I want to put the name of the current user that is logged  
> in.
> 
> I could do something like @session[:username] but I would rather  
> encapsulate that into a single variable so that I can change the key in  
> the session map if I want without having to goto each page that uses it.

What about something like this:

/app/helpers/application\_helper.rb  
def username  
if @session.nil?  
“Guest”  
else  
@session[:username]  
end  
end

/app/views/layouts/application.rhtml

\<%= username %\>

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/5 "2006-01-04T18:13:10Z")

</div>

Gareth R. wrote:

> It seems that a better way would be to use an instance variable like I

class ApplicationController \< ActionController::Base  
before\_filter :setup\_user  
private  
def setup\_user  
@user = @session[‘user’]  
end  
end

–  
For rails performance tuning, see: [http://railsexpress.de/blog](http://railsexpress.de/blog)  
Subscription: [http://railsexpress.de/blog/xml/rss20/feed.xml](http://railsexpress.de/blog/xml/rss20/feed.xml)

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/6 "2006-01-04T19:45:17Z")

</div>

On Jan 4, 2006, at 8:31 AM, Lance B. wrote:

> end
> 
> /app/views/layouts/application.rhtml
> 
> \<%= username %\>

Note that the above won’t work all the time. You need to check for  
session[:username].nil? and not just session.nil? because the flash  
and all kinds of other things will go into the session making it not  
nil most of the time. SO be careful with that.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/7 "2006-01-04T20:11:36Z")

</div>

Why not use a partial template for the user information? So long as you  
include that, all you would need to change is the partial and everything  
else will update.

\<%= render(:partial=\>‘shared/userinfo’) %\>

Just put \_userinfo.rhtml in app/views/shared

---

<div class="post-metadata">

**Author:** ![reevesg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@reevesg](https://www.ruby-forum.com/u/reevesg)\
**Post date:** [January 4, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/variables-in-layouts/51618/8 "2006-01-04T20:23:02Z")

</div>

Kevin O. wrote:

> Why not use a partial template for the user information? So long as you  
> include that, all you would need to change is the partial and everything  
> else will update.
> 
> \<%= render(:partial=\>‘shared/userinfo’) %\>
> 
> Just put \_userinfo.rhtml in app/views/shared

These are all great suggestions guys and exactly what I was looking for.  
I particularly like the helper suggestion and think that will suit my  
needs best for the moment and then maybe move to the partial template.

Thanks again.

Gareth
