Mayu
Mayu

Reputation: 63

ruby template erb

I have a layout.html.erb file w hich should act as a common file for all the pages to decorate as shown below.

<%= render :partial => "layouts/header" %> 
  <%= render :partial => "layouts/leftsidemenu" %>
  <body>
    <%= @content_for_layout %>
  </body>
<%= render :partial => "layouts/footer" %>

How can I configure this rails framework so that, so that I do not want to include layout.html.erb in all the pages as

<%= render :partial => "layouts/layout" %> 

I need to configuration file to decorate, as we do in Struts framework using sitemesh decorator.xml file.

thanks in advance Mahesh

Upvotes: 0

Views: 733

Answers (2)

MBO
MBO

Reputation: 30985

First of all, default layout for Rails app is in <rails_app>/app/views/layouts/application.html.erb and is used because all your controllers inherit from ApplicationController (see the name, as convention Rails use layout with same base name as controller, or name of parent controller and so on).

Second, your layout should look something like that:

<%= render :partial => "header" %> 
<%= render :partial => "leftsidemenu" %>
<body>
  <%= yield %>
</body>
<%= render :partial => "footer" %>

of even paste content from header and footer to this layout. More informations about layouts you can find in this guide.

If you want to change some aspect of page, for example title, then you can do so with layouts too:

# header.html.erb
<head>
  <title>
    <%= yield(:title) of "Default title" %>
  </title>
</head>

# page.html.erb
<% content_for :title do %>
  Specific title
<% end %>

Page content

If you want to use layout from different file, then you can do so that way:

# ApplicationController.rb

class ApplicationController < ActionController::Base
  # ...
  layout 'your_layout' # file in app/views/layouts
  # ...
end

Upvotes: 2

Janteh
Janteh

Reputation: 398

Take a look at the Rails docs for structuring layouts, especially the yield and content_for tags. You can also specify a layout with layout in a controller, or a default one for all controllers in your application controller.

Upvotes: 1

Related Questions