nice_partials
nice_partials copied to clipboard
Customisation with options
Closes #13
This pull request adds a simple customisation enhancement. It allows Nice Partials to provide a default options object to the parent view via np
, and made accesible from p.options
. This can be modified by the parent view as needed, effectively allowing it to "reach in" and modify the internals of the partial for a given case.
For example, classes can be modified for that occasional case that needs different: in this case, adding rounded-none
and removing rounded
:
# app/views/posts/index.html.erb
<%= render 'components/card', class: 'rounded-none' do |p| %>
<% p.options[:class].delete('rounded') %>
…
<% end %>
# app/views/components/_card.html.erb
<% yield p = np(class: ['card', 'rounded', local_assigns[:class]]) %>
<div class="<%= token_list(p.options[:class]) %>">
…
</div>
What's happening here?
- The card partial sets up the default options hash with class names, merging in those passed in as locals.
- The index template passes in
rounded-none
as a local, then deletes therounded
class - The card partial sets the class attribute with the now-modified
options[:class]
usingtoken_list
in Rails 6.1