sa 125 wrote:
> Those were really helpful posts -- I think I got the general idea, in 
> which I should specify in the controller which layout it should belong 
> to, and then in the layout file use <%= yield %> to render these views. 
> I'll keep playing with it and see what comes up.
> 
> Thanks!

Take this example:

app/controllers/people_controller.rb:

class PeopleController < ApplicationController
  # layout 'custom'

  # GET /people
  # GET /people.xml
  def index
    @people = Person.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @people }
    end
  end
end

app/views/layouts/application.html.erb:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>My Application</title>
</head>
<body>
  <h1>Header in Application layout</h1>
  <%= yield %>
  <p>Footer in layout</p>
</body>
</html>

app/views/layouts/people.html.erb:
----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>My Application</title>
</head>
<body>
  <h1>Header in People layout</h1>
  <%= yield %>
  <p>Footer in layout</p>
</body>
</html>
----

app/views/layouts/custom.html.erb:
----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>My Application</title>
</head>
<body>
  <h1>Header in Custom layout</h1>
  <%= yield %>
  <p>Footer in layout</p>
</body>
</html>
----

app/views/people/index.html.erb:
----
<h1>Listing people</h1>

<table>
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>

  <% for person in @people %>
  <%= render :partial => 'list' %>
  <% end %>
</table>

<br />

<%= link_to 'New person', new_person_path %>
----

Notice in this example that the first line in the people_controller is 
commented out. So in this case Rails begins searching for an appropriate 
layout by first looking in app/layouts/ for a layout template named 
people.html.erb. If it exists Rails will use it automatically. If it 
does not exist then Rails looks for a template named 
application.html.erb. If that file exists it will be applied to any 
controller that does not specify a specific layout to use.

In all cases if the layout is provided (by uncommenting line #1 in this 
example controller) it will look for a template with the given name 
(custom.html.erb) in this example.

I also provided a contrived partial in the 
app/views/people/index.html.erb template to show when how how partials 
might be used.

There is also a method called "content_for" I'll leave that up to you to 
investigate, but this can also be very useful when dealing with layouts 
and partials.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to