Dynamic layout

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?

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:

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.

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, 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… :slight_smile:

Emmanuel O.

Alright, thanks a lot :slight_smile: 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 =)

Also read this blog post:

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.