Rendering partials with layouts

Module ActionView::Partials

引用
Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally
for the entire action, but they work in a similar fashion. Imagine a list with two types of users:

   <!-- app/views/users/index.html.erb -->
   Here's the administrator:
   <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>


   Here's the editor:
   <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>


   <!-- app/views/users/_user.html.erb -->
   Name: <%= user.name %>


   <!-- app/views/users/_administrator.html.erb -->
   <div id="administrator">
     Budget: $<%= user.budget %>
     <%= yield %>
   </div>


   <!-- app/views/users/_editor.html.erb -->
   <div id="editor">
     Deadline: $<%= user.deadline %>
     <%= yield %>
   </div>


...this will return:

   Here's the administrator:
   <div id="administrator">
     Budget: $<%= user.budget %>
     Name: <%= user.name %>
   </div>


   Here's the editor:
   <div id="editor">
     Deadline: $<%= user.deadline %>
     Name: <%= user.name %>
   </div>

You can also apply a layout to a block within any template:

   <!-- app/views/users/_chief.html.erb -->
   <% render(:layout => "administrator", :locals => { :user => chief }) do %>
     Title: <%= chief.title %>
   <% end %>


...this will return:

   <div id="administrator">
     Budget: $<%= user.budget %>
     Title: <%= chief.name %>
   </div>

你可能感兴趣的:(html)