README.md in nice_partials-0.1.2 vs README.md in nice_partials-0.1.3

- old
+ new

@@ -1,28 +1,28 @@ # nice_partials [![[version]](https://badge.fury.io/rb/nice_partials.svg)](https://badge.fury.io/rb/nice_partials) [![[travis]](https://travis-ci.org/andrewculver/nice_partials.svg)](https://travis-ci.org/andrewculver/nice_partials) -Nice Partials provides a light layer of magic on top of traditional Rails view partials to try and make them an even better fit for extracting and reusing components in your views. +Nice Partials extends the concept of [`content_for` blocks and `yield`](https://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method) for those times when a partial needs to provide one or more named "content areas" or "slots". This thin, optional layer of magic helps make traditional Rails view partials an even better fit for extracting components from your views, like so: -It allows your partials to define named content areas like this: - -`app/views/partials/_card.html.erb`: +`app/views/components/_card.html.erb`: ```html+erb <div class="card"> <%= p.yield :image %> <div class="card-body"> <h5 class="card-title"><%= title %></h5> - <p class="card-text"> - <%= p.yield :body %> - </p> + <% if p.content_for? :body %> + <p class="card-text"> + <%= p.yield :body %> + </p> + <% end %> </div> </div> ``` These partials can still be utilized with a standard `render` call, but you can specify how to populate the content areas like so: ```html+erb -<%= render 'partials/card', title: 'Some Title' do |p| %> +<%= render 'components/card', title: 'Some Title' do |p| %> <% p.content_for :body do %> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, <strong>quis nostrud exercitation ullamco laboris</strong> nisi ut aliquip ex ea commodo consequat. @@ -48,28 +48,26 @@ - allows isolated helper logic alongside your partial view code. - doesn't require any upgrades to existing partials for interoperability. - are still testable! -## How does it work? +## Can't I do the same thing without Nice Partials? -Nice Partials slightly extends the concept of [`content_for` blocks and `yield`](https://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method) so they can be properly used to define and utilize "content areas" or "slots" in simple ERB partials. - -### Can't I do the same thing without Nice Partials? - You can almost accomplish the same thing without Nice Partials, but in practice you end up having to flush the content buffers after using them, leading to something like this: ```html+erb <div class="card"> <%= yield :image %> <% content_for :image, flush: true do '' end %> <div class="card-body"> <h5 class="card-title"><%= title %></h5> - <p class="card-text"> - <%= yield :body %> - <% content_for :body, flush: true do '' end %> - </p> + <% if content_for? :body %> + <p class="card-text"> + <%= yield :body %> + <% content_for :body, flush: true do '' end %> + </p> + <% end %> </div> </div> ``` Earlier iterations of Nice Partials aimed to simply clean up this syntax with helper methods like `flush_content_for`, but because the named content buffers were in a global namespace, it was also possible to accidentally create situations where two partials with a `:body` content area would end up interfering with each other, depending on the order they're nested and rendered. @@ -89,11 +87,11 @@ ### When to use Nice Partials You only need to use Nice Partials when: - - you want to define multiple named content areas in your partial. If you don't have multiple named content areas in your partial, you could just pass your content into the partial using the standard block and `yield` approach. + - you want to define one or more named content areas in your partial. If you don't have multiple named content areas in your partial, you could just pass your content into the partial using the standard block and `yield` approach. - you want to specifically isolate your helper methods for a specific partial. ### Use Nice Partials in a partial @@ -113,10 +111,10 @@ (This is, [as far as we know](https://github.com/bullet-train-co/nice_partials/issues/1), the minimum viable invocation.) Once you've done this at the top of your partial file, you can then use `<%= p.yield :some_section %>` to render whatever content areas you want to be passed into your partial. -### Extra Credit: Defining and Using Well Isolated Helper Methods +### Defining and using well isolated helper methods To minimize the amount of pollution in the global helper namespace, you can use the shared context object to define helper methods specifically for your partials _within your partial_ like so: ```html+erb <% p.helpers do