v0.9.0
What's Changed
-
Fix rendering with special characters in a view path.
Ref: #70
-
Seed Nice Partials content from
local_assigns
Previously, the only way to assign content to a Nice Partial was through passing a block:
# app/views/posts/show.html.erb <%= render "posts/post", byline: "Some guy" %> # app/views/posts/_post.html.erb <%= render "card" do |partial| %> <% partial.title "Hello there" %> <% partial.byline byline %> <%# `byline` comes from the outer `render` call above. %> <% end %> Now, Nice Partials will automatically use Rails' `local_assigns`, which contain any `locals:` passed to `render`, as the seed for content. So this works: ```erb <%= render "card", title: "Hello there", byline: byline %>
And the
card
partial is now oblivious to whether itstitle
orbyline
were passed as renderlocals:
or through the usual assignments in a block.# app/views/_card.html.erb <%= partial.title %> written by <%= partial.byline %>
Previously to get this behavior you'd need to write:
# app/views/_card.html.erb <%= partial.title.presence || local_assigns[:title] %> written by <%= partial.byline.presence || local_assigns[:byline] %>
Passing extra content via a block appends:
<%= render "card", title: "Hello there" do |partial| %> <% partial.title ", and welcome!" %> # Calling `partial.title` outputs `"Hello there, and welcome!"` <% end %>
-
Add
NicePartials::Partial#slice
Returns a Hash of the passed keys with their contents, useful for passing to other render calls:
<%= render "card", partial.slice(:title, :byline) %>
-
Fix
partial.helpers
accidentally adding methods toActionView::Base
When using
partial.helpers {}
, internallyclass_eval
would be called on the Partial instance, and throughdelegate_missing_to
passed on to the view context and thus we'd effectively have a global method, exactly as if we'd just used regular Rails view helpers. -
Let partials respond to named content sections
<% partial.content_for :title, "Title content" %> # Before <% partial.title "Title content" %> # After # Which can then be output <% partial.title %> # => "Title content" <% partial.title? %> # => true
Note,
title
responds topresent?
so rendering could also be made conditional with:<% partial.title if partial.title? %> # Instead of this… <% partial.title.presence %> # …you can do this
Passing procs or components
Procs and objects that implement
render_in
, like ViewComponents, can also be appended as content:<% partial.title { "some content" } %> <% partial.title TitleComponent.new(Current.user) %>
Capturing
options
Options can also be captured and output:
<% partial.title class: "text-m4" %> # partial.title.options # => { class: "text-m4" } # When output `to_s` is called and options automatically pipe through `tag.attributes`: <h1 <% partial.title.options %>> # => <h1 class="text-m4">
Proxying to the view context and appending content
A content section appends to its content when calling any view context method on it, e.g.:
<% partial.title.t ".title" %> <% partial.title.link_to @document.name, @document %> <% partial.title.render "title", user: Current.user %> <% partial.title.render TitleComponent.new(Current.user) do |component| %> <% … %> <% end %>
Building elements with
tag
proxyThese
tag
calls let you generate elements based on the stored content and options:<% partial.title "content", class: "post-title" %> # Adding some content and options… <% partial.title.h2 %> # => <h2 class="post-title">content</h2> <% partial.title.h2 "more" %> # => <h2 class="post-title">contentmore</h2>
-
Add
NicePartials#t
to aid I18n.When using NicePartials with I18n you end up with lots of calls that look like:
<% partial.title t(".title") %> <% partial.description t(".header") %> <% partial.byline t("custom.key") %>
With NicePartials'
t
method, you can write the above as:<% partial.t :title, description: :header, byline: "custom.key" %>
Clarifying what keys get converted to what content sections on the partial rather than the syntax heavy
partial.… t(".…")
.Like the Rails built-in
t
method, it's just a shorthand alias fortranslate
so that's available too. -
Add
Partial#content_from
content_from
lets a partial extract contents from another partial.
Additionally, contents can be renamed by passing a hash:<% partial.title "Hello there" %> <% partial.byline "Somebody" %> <%= render "shared/title" do |cp| %> # Here the inner partial `cp` accesses the outer partial through `partial` # extracting the `title` and `byline` contents. # `byline` is renamed to `name` in `cp`. <% cp.content_from partial, :title, byline: :name %> <% end %>
What's Changed by PRs
- Reuse ActionView::TestCase for testing by @kaspth in #25
- Remove unneeded Action View patches for xray-rails by @kaspth in #28
- Clarify translation context popping by @kaspth in #26
- Store t prefix on the stack by @kaspth in #30
- Remove unneeded
render
override by @kaspth in #31 - Replace content_for backing with OutputFlow implementation by @kaspth in #42
- Replace
p
withpartial
as the default variable? by @andrewculver in #37 - Base auto-capture logic on yield calls in template source by @kaspth in #45
- Allow yielding arguments to
content_for
blocks by @kaspth in #43 - Use load hooks for monkey_patch loading by @kaspth in #48
- Fix accessing
partial
before rendering leaks state by @kaspth in #47 - Allow outer partial access when capturing by @kaspth in #49
- Rename
Partial#output_buffer
toPartial#yield
by @seanpdoyle in #41 - Rename
Partial#output_buffer
toPartial#yield
(#41) by @andrewculver in #51 - Document release 🎉 by @kaspth in #52
- Add
content_from
to let partials relay contents by @kaspth in #53 - Introduce Capybara to the test suite by @seanpdoyle in #55
- Expose Sections as an alternative to
content_for
by @kaspth in #57 - Mixing
yield
call styles by @seanpdoyle in #54 - Skip needless CI steps by @kaspth in #60
- Bugfix: Partial Options Nil Pointer Exception by @seanpdoyle in #61
- Test passing one section another's writer works by @kaspth in #63
- Rewrite README post overhaul by @kaspth in #62
- Update with View Component 2.7.5+ support in tests by @kaspth in #66
- Add test coverage for Partial Helpers by @kaspth in #65
- Let
local_assigns
seed content by @kaspth in #68 - Fix special locale prefixes with special characters in view_paths by @kaspth in #70
- Test local assigns content seeds support strict locals by @kaspth in #71
- Remove public
output_buffer
accessor by @kaspth in #72 - Prep v0.9.0 by @kaspth in #74
Full Changelog: v0.1.5...v0.9.0