# DRY question (newbie)

**URL:** <https://www.ruby-forum.com/t/dry-question-newbie/99726>\
**Category:** Rails\
**Created:** [July 25, 2006, 5:02am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726 "2006-07-25T05:02:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:02am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/1 "2006-07-25T05:02:09Z")

</div>

Say i want to include the same chunck of text (a menu on a webpage for  
example) on each page. Is there a method by which I can save the HTML in  
a text file and call it up later? Is there a better way?

I know ruby has “puts” but i can’t use that b/c it does not work with my  
webhost ([dreamhost.com](http://dreamhost.com))

Any suggestions?

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:13am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/2 "2006-07-25T05:13:47Z")

</div>

For a menu you would usually put that in a layout, then in your  
controllers  
you can specify that layout using…

layout “mylayout”

Inside your layout you would do something like…

...head stuff... ...menu bar html... \<%= yield %\>

The \<%= yield%\> line puts what would usually be appearing without a  
layout  
in its place.

Good Luck!

~Jamie  
[www.jamiequint.com](http://www.jamiequint.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:17am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/3 "2006-07-25T05:17:10Z")

</div>

If you want the menu system to be available to your entire application,  
put  
it in /app/views/layouts/application.rhtml. It will automatiaclly be  
used by  
all controllers without having to use the layout command at the top of  
each  
controller.

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:23am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/4 "2006-07-25T05:23:42Z")

</div>

For something like a nav, that will appear on all pages, a layout is  
what you want to use, like Jamie mentioned.

If you have a chunk of html that will be used on some pages, but not  
all, look into using a partial.

-TJ

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:17am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/5 "2006-07-25T05:17:00Z")

</div>

You want layouts…

Create ./app/views/layouts/application.rhtml

Try something like

* * *

Hello World from the header!  
\<%= @content\_for\_layout %\>  
Hello world form the footer!

* * *

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 6:41am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/6 "2006-07-25T06:41:59Z")

</div>

On 7/25/06, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Cheers

I probably should add that this method is only really useful when the  
menu  
changes per view.

Otherwise as others have suggested, stick it in the layout or a partial.

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [July 25, 2006, 5:26am UTC](https://www.ruby-forum.com/t/dry-question-newbie/99726/7 "2006-07-25T05:26:55Z")

</div>

On 7/25/06, Stephen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello world form the footer!
> 
> > ## example) on each page. Is there a method by which I can save the HTML in
> 
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Another alternative to this is to use the content\_for method

In you view

\<% content\_for( “menu”) do %\>  
erb for menu here  
\<% end %\>

and then in your layout

\<%= yield :menu %\>

This should put your menu code in. If it’s not right I’ll check then  
syntax when I get home

Cheers
