提交 f02a35b8 编写于 作者: N Nikolay Shebanov

Make possible to use blocks with short version of render partial

上级 10e989b7
* Make possible to use blocks with short version of `render "partial"` helper.
*Nikolay Shebanov*
Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/actionview/CHANGELOG.md) for previous changes.
......@@ -32,7 +32,7 @@ def render(options = {}, locals = {}, &block)
view_renderer.render(self, options)
end
else
view_renderer.render_partial(self, :partial => options, :locals => locals)
view_renderer.render_partial(self, :partial => options, :locals => locals, &block)
end
end
......
<%= render "test/layout_for_block_with_args" do |arg_1, arg_2| %>
Yielded: <%= arg_1 %>/<%= arg_2 %>
<% end %>
......@@ -466,6 +466,11 @@ def test_render_partial_and_layout_without_block_with_locals_and_rendering_anoth
@view.render(:partial => 'test/partial_with_partial', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
end
def test_render_partial_shortcut_with_block_content
assert_equal %(Before (shortcut test)\nBefore\n\n Yielded: arg1/arg2\n\nAfter\nAfter),
@view.render(partial: "test/partial_shortcut_with_block_content", layout: "test/layout_for_partial", locals: { name: "shortcut test" })
end
def test_render_layout_with_a_nested_render_layout_call
assert_equal %(Before (Foo!)\nBefore (Bar!)\npartial html\nAfter\npartial with layout\n\nAfter),
@view.render(:partial => 'test/partial_with_layout', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
......
......@@ -1025,6 +1025,42 @@ One way to use partials is to treat them as the equivalent of subroutines: as a
Here, the `_ad_banner.html.erb` and `_footer.html.erb` partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
As you already could see from the previous sections of this guide, `yield` is a very powerful tool for cleaning up your layouts. Keep in mind that it's pure ruby, so you can use it almost everywhere. For example, we can use it to DRY form layout definition for several similar resources:
* `users/index.html.erb`
```html+erb
<%= render "shared/search_filters", search: @q do |f| %>
<p>
Name contains: <%= f.text_field :name_contains %>
</p>
<%= end %>
```
* `roles/index.html.erb`
```html+erb
<%= render "shared/search_filters", search: @q do |f| %>
<p>
Title contains: <%= f.text_field :title_contains %>
</p>
<%= end %>
```
* `shared/_search_filters.html.erb`
```html+erb
<%= form_for(@q) do |f| %>
<h1>Search form:</h1>
<fieldset>
<%= yield f %>
</fieldset>
<p>
<%= f.submit "Search" %>
</p>
<% end %>
```
TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
#### Partial Layouts
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册