# Sharing code in views

**URL:** <https://www.ruby-forum.com/t/sharing-code-in-views/112541>\
**Category:** Rails\
**Created:** [August 20, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/sharing-code-in-views/112541 "2007-08-20T19:55:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gandalf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/59ef9b/32.png) [@Gandalf](https://www.ruby-forum.com/u/Gandalf)\
**Post date:** [August 20, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/sharing-code-in-views/112541/1 "2007-08-20T19:55:55Z")

</div>

I am new to Rails and am wondering how to share code across views. I  
have two views and want to share website logo & header. Any thoughts  
would be very helpful.

Thanks.

---

<div class="post-metadata">

**Author:** ![Gandalf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/59ef9b/32.png) [@Gandalf](https://www.ruby-forum.com/u/Gandalf)\
**Post date:** [August 20, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/sharing-code-in-views/112541/2 "2007-08-20T20:26:03Z")

</div>

You need to use layouts. Create one called application.rhtml which is  
where you keep the site-wide html. For an excellent introduction to  
layouts, see this screencast:

> **[\#7 All About Layouts - RailsCasts](http://railscasts.com/episodes/7-all-about-layouts)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Hope that helps,

DAZ

---

<div class="post-metadata">

**Author:** ![Gandalf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/59ef9b/32.png) [@Gandalf](https://www.ruby-forum.com/u/Gandalf)\
**Post date:** [August 20, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/sharing-code-in-views/112541/3 "2007-08-20T23:03:25Z")

</div>

Use a partial. It’s just a file that starts with an underscore in your  
views folder. And then call it via render :partial =\> :logo

All partials & views can access instance variables set in your  
controller. Partials can also be passed local variables through a  
:locals hash.

[http://railsbrain.com/api/rails-1.2.3/doc/index.html?a=M000463&name=render](http://railsbrain.com/api/rails-1.2.3/doc/index.html?a=M000463&name=render)

Vish

---

<div class="post-metadata">

**Author:** ![Gandalf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/59ef9b/32.png) [@Gandalf](https://www.ruby-forum.com/u/Gandalf)\
**Post date:** [August 21, 2007, 12:40am UTC](https://www.ruby-forum.com/t/sharing-code-in-views/112541/4 "2007-08-21T00:40:40Z")

</div>

These are the most common and simple ways of sharing code:

You have two methods, depending on what you want to achieve. A  
layout under app/views/layout allows you to declare a page that  
serves as a shell for your content and then you can add a  
content\_for\_layout reference where you want your content to appear.  
You can even have multiple layouts. Another way to share code is to  
create partials, such as a common header or footer, that your views  
can include.

Paul
