Paritosh Singh
Paritosh Singh

Reputation: 6384

how to extend child template to parent in rails

I am new to rails. I am having difficulty in understanding template inheritance. Earlier I have worked in django and seen template inheritence there. There I saw child is told about parent using "extends" command. Can anyone explain how it works here. I have gone through guidelines of ruby but it was not clear.

Thanks

Upvotes: 3

Views: 1147

Answers (1)

Tigraine
Tigraine

Reputation: 23648

It's quite simple to do in Rails.

You simply tell the template you are currently rendering to render another template.

For example layouts/application.html.erb contains something like this:

<% content_for :navigation do %>
<nav>...</nav>
<% end %>     

<% content_for :content do %>                                                                                                                                                                       
<%= yield %>                                                                                                                                                                                        
<% end %>                                                                                                                                                                                           

<%= render :template => 'layouts/main_application' %>   

The important part is the render :template part that then delegates this template to also render the layouts/main_application.html.erb that in my case looks something like this:

<header>
...
</header>
<body>
<%= yield :nav %>
<%= content_for?(:content) ? yield(:content) : yield %> 
</body>

What I am doing here is having a main template that does not contain the navigation (for things like login etc) and the application.html.erb adds that navigation to the :nav content placeholder.

Upvotes: 1

Related Questions