# Dynamic layout

**URL:** <https://www.ruby-forum.com/t/dynamic-layout/106968>\
**Category:** Rails\
**Created:** [June 15, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968 "2007-06-15T22:18:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakedgr8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jakedgr8](https://www.ruby-forum.com/u/jakedgr8)\
**Post date:** [June 15, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968/1 "2007-06-15T22:18:47Z")

</div>

Well I’m new to Ruby on Rails and actually new to implementing xhtml  
layout into my apps. So right now I’m making each page a layout, but the  
layout is the same xhtml for every single page. The problem is I have a  
menu, and the links for the menu are changing. The copyright notice  
could change, whatever it is. And so I have to change the layout of  
every single page. Is there a way I can store the layout in a location  
and get rails to retrieve that layout and implement it into the page  
(sort of like an “include”). And on this layout I have a title in the

 tags. Each page has a different title, so is there I way I can make the title different for each page? And how would I do all this?

---

<div class="post-metadata">

**Author:** ![jakedgr8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jakedgr8](https://www.ruby-forum.com/u/jakedgr8)\
**Post date:** [June 15, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968/2 "2007-06-15T22:42:01Z")

</div>

Jake F. wrote:

> Well I’m new to Ruby on Rails and actually new to implementing xhtml  
> layout into my apps. So right now I’m making each page a layout, but the  
> layout is the same xhtml for every single page. The problem is I have a  
> menu, and the links for the menu are changing. The copyright notice  
> could change, whatever it is. And so I have to change the layout of  
> every single page. Is there a way I can store the layout in a location  
> and get rails to retrieve that layout and implement it into the page  
> (sort of like an “include”). And on this layout I have a title in the
> 
> tags. Each page has a different title, so is there I way I can make the title different for each page? And how would I do all this?

Also be sure to investigate the use of css for customizing your pages:

> **[CSS Zen Garden: The Beauty of CSS Design](http://www.csszengarden.com/)**
>
> A demonstration of what can be accomplished visually through CSS-based design.

There, a single page content is styled in a lot of different ways, with  
amazing results! You can implement that in rails using different css  
files in the layout, and changing the css using the same technique as  
with the title solution.

---

<div class="post-metadata">

**Author:** ![jakedgr8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jakedgr8](https://www.ruby-forum.com/u/jakedgr8)\
**Post date:** [June 15, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968/3 "2007-06-15T22:37:01Z")

</div>

Jake F. wrote:

> Well I’m new to Ruby on Rails and actually new to implementing xhtml  
> layout into my apps. So right now I’m making each page a layout, but the  
> layout is the same xhtml for every single page. The problem is I have a  
> menu, and the links for the menu are changing. The copyright notice  
> could change, whatever it is. And so I have to change the layout of  
> every single page. Is there a way I can store the layout in a location  
> and get rails to retrieve that layout and implement it into the page  
> (sort of like an “include”). And on this layout I have a title in the
> 
> tags. Each page has a different title, so is there I way I can make the title different for each page? And how would I do all this?

mmmm i don’t quite get the idea… but let’s guess you wan’t to make in  
the same application several different looks. You have two levels of  
abstraction for your pages.

1. Layouts. They appear in app/views/layouts. Simply make a layout with  
the common things in every page. For example, here is a main.html file  
in the layouts dir:

 \<%= @title\> \<%= javascript\_include\_tag :defaults %\> \<%= yield :layout %\> 

Notice two things:

First, \<%= @title\>. Now in your controller, you can set  
an @title variable and assign to it whatever title you want.

Second: \<%= yield :layout %\>. This means: Put in here te generated page  
template. What generated page template? That depends on your controller.  
Lets say you have a “Main” Controller:

class MainController \< ApplicationController  
layout ‘main’ # As rails uses convention over configuration, this is  
not  
# a necesity, why? because the template has the same  
name  
# as the controller, but you can put it or change it if  
# you want. i.e: create a foo.rhtml page in the  
# layout dir, then put layout ‘foo’ and tada!

def index  
@title= “THIS IS THE TITLE OF THE PAGE”  
end  
end

Now, if you browse to your application url, let’s say  
[http://localhost:3000/main/index](http://localhost:3000/main/index), you acces to your Main Controller, the  
index action. So rails looks for a index.rhtml file in the  
app/views/main directory, renders that file and the result is put on the  
place you indicated with the  
\<%= yield :layout %\> syntax.

So, you want to put a different look for every page? Create a controller  
for every page you want, make as many layouts as necesary to change the  
looks, then work in the templates for the action in every controller.

I think that’s it… 🙂

Emmanuel O.

---

<div class="post-metadata">

**Author:** ![jakedgr8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jakedgr8](https://www.ruby-forum.com/u/jakedgr8)\
**Post date:** [June 15, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968/4 "2007-06-15T22:51:10Z")

</div>

Alright, thanks a lot 🙂 I knew the layouts thing in number one but  
number two sor of opened a new door into rails for me. Thanks for all  
your help =)

---

<div class="post-metadata">

**Author:** ![jakedgr8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jakedgr8](https://www.ruby-forum.com/u/jakedgr8)\
**Post date:** [June 16, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/dynamic-layout/106968/5 "2007-06-16T14:11:31Z")

</div>

Also read this blog post:

> **[Strut Your Structs — err.the\_blog](http://errtheblog.com/post/28)**
>
> Err the Blog: Ruby and Rails from the front.

It shows many good exampls on how to implement changing sidebars (e.g.  
wih different links as you said)  
for each controller or action into one single main layout.
