Just started Ruby on Rails yesterday. In my layouts/application.html.erb i have:
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
Coming from php ->codeigniter background, i'm assuming render is similar to $this->load->view(''); in codeigniter.
Although this works fine, i would like to have multiple application layout files e.g
In codeigniter you simply declare which of the templates/layout files you wish to use but as ruby on rails is a bit magical(It does a lot of things for you), i'm assuming it calls the application layout by default. I was wondering if there's a way to choose what layout file i want?
Rails provides us great functionality for managing layouts in a web application. The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.
In Rails, layouts are pieces that fit together (for example header, footer, menus, etc) to make a complete view. An application may have as many layouts as you want. Rails use convention over configuration to automatically pair up layouts with respective controllers having same name.
By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.
A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.
@Deefour gave the right resources, here is a nice quick example on how you can implement this in Rails 4.
In a controller you can specify where you want to fetch what layout for a certain action and have very nicely fine grained control over which layout is used.
class PagesController < ApplicationController
layout "fullwidth", except: [:index, :faqs]
def popout
# Render this action with specific layout
render layout: "popout"
#renders with views/layouts/popout.html.erb
end
def index
#renders with views/layouts/application.html.erb
end
def about_us
#renders with views/layouts/fullwidth.html.erb
end
def team
#renders with views/layouts/fullwidth.html.erb
end
def logo
#renders with views/layouts/fullwidth.html.erb
end
def faqs
#renders with views/layouts/application.html.erb
end
end
application.html.erb is the rails standard layout file. I assume it is present, and it is the default fallback!
You're looking for the layout
method.
This Rails Guide will help you, specifically Finding Layouts. I'd provide more detail here, but the previously mentioned documentation and guide provide more than enough examples and usage instruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With