Rendering partials with layouts

Module ActionView::Partials

[quote]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:
[code] <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>[/code]

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

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

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

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

...this will return:

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

Here's the editor:
[code] <div id="editor">
Deadline: $<%= user.deadline %>
Name: <%= user.name %>
</div>
[/code]
You can also apply a layout to a block within any template:

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

...this will return:

[code] <div id="administrator">
Budget: $<%= user.budget %>
Title: <%= chief.name %>
</div>[/code][/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值