Forum: Ruby on Rails dynamic layout

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4fb768311b3974b994dbf17bfc4c79ef?d=identicon&s=25 Jacob J. (jakedgr8)
on 2007-06-15 22:18
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
<title></title> 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?
B3086d7340aeba221e30ce8679eab3f3?d=identicon&s=25 Emmanuel Oga (emmanueloga)
on 2007-06-15 22:37
Jake Fake 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
> <title></title> 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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="content-type" content="application/xhtml+xml;
charset=utf-8" />
    <meta name="author" content="Emmanuel Oga" />
    <title><%= @title></title>
    <%= javascript_include_tag :defaults %>
  </head>

  <body>
       <%= yield :layout %>
  </body>
</html>

Notice two things:

First, <title><%= @title></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... :)

Emmanuel Oga
B3086d7340aeba221e30ce8679eab3f3?d=identicon&s=25 Emmanuel Oga (emmanueloga)
on 2007-06-15 22:42
Jake Fake 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
> <title></title> 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:

http://www.csszengarden.com/

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.
4fb768311b3974b994dbf17bfc4c79ef?d=identicon&s=25 Jacob J. (jakedgr8)
on 2007-06-15 22:51
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 =)
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-06-16 14:11
(Received via mailing list)
Also read this blog post:
http://errtheblog.com/post/28

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.
This topic is locked and can not be replied to.