一点魔法,使部分元素完美适用于组件。
Nice Partials adds ad-hoc named content areas, or sections, to Action View partials with a lot of extra power on top.
Everything happens through a new partial method, which at the base of it have method shorthands for partial specific content_for and content_for?s.
See, here we're outputting the image, title, and body sections:
app/views/components/_card.html.erb:
Then in render we populate them:
# Same as `partial.content_for :title, t(".title")`
Lorem ipsum dolor sit amet, …
So far these uses are pretty similar to Rails' global content_for & content_for?, except these sections are local to the specific partial, so there's no clashing or leaking.
Consider this regular Rails partials rendering:
# app/views/components/_card.html.erb
There's a number of gotchas here:
content_for writes to :title across every partial, thus leaking.is, so the `content_for` isn't called and outputs nothing.With Nice Partials the yield is automatic and we can write content for just that partial without leaking:
# app/views/components/_card.html.erb
This happens because Nice Partials checks the partial source code for any yield calls that calls Rails' capture helper — e.g. yield and yield something but not yield :title. If there's no capturing yields Nice Partials calls capture for you.
This means Nice Partials also respect existing yield calls in your partial, so you can upgrade existing partials bit by bit or not at all if you don't want to.
Nice Partials:
Nice Partials are a lightweight and more Rails-native alternative to ViewComponent. Providing many of the same benefits as ViewComponent with less ceremony.
partial give me?Having a partial object lets us add abstractions that are hard to replicate in standard Rails partials.
Nice Partials will use Action View's local_assigns, which stores any locals passed to render, as the basis for contents.
Given a partial like
written by Can then be used like this:
This will then output "Hello there written by Some guy, who writes stuff"
You can also use slice to pass on content from an outer partial:
In traditional Rails partials, you'll see lots of checks for whether or not we have content to then output an element.
With Nice Partials, it would look like this:
However, we can remove the conditional using optional:
This will avoid outputting an empty tag, which could mess with your markup, in case there's no content provided for title.
Note: with Nice Partials tag helpers support, this example could also be shortened to ``.
Alternatively, if title is a section that we require to be provided, we can do:
Here, required will raise in case there's been no title content provided by that point.
Nice Partials supports calling any method on ActionView::Base, like the helpers shown here, and then have them auto-append to the section.
partial.t is a shorthand to translate and assign multiple keys at once:
# The above is the same as writing:
You can pass keyword options to a writer method and they'll be auto-added to partial.x.options, like so:
# app/views/components/_card.html.erb:
# From the render above `title.options` now contain `{ class: "text-m4", data: { controller: "title" } }`.
# The options can be output via `
# =>
Title content
partial also supports auto-generating an element by calling any of Rails' tag methods e.g.:
# This shorthand gets us the same h1 element from the previous example:
# =>
Title content
# Internally, this is similar to doing:
The above example showed sending options from the rendering block into the partial and having it construct elements.
But the partial can also prepare tag builders that the rendering block can then extend and finalize:
# app/views/components/_card.html.erb
# =>
Title content
partial.yieldTo access the inner content lines in the block here, partials have to manually insert a `` call.
Some content!
Yet more content!
With Nice Partials, partial.yield returns the same content:
# app/views/components/_card.html.erb
# => "Some content!\n\nYet more content!"During a rendering block partial refers to the outer partial, so you can compose them.
If you need to pass content into another partial, content_from lets you pass the keys to extract and then a hash to rename keys.
Here, we copied the partial.title to cp.title and partial.byline became cp.header.
If you want to have helper methods that are available only within your partials, you can call partial.helpers directly:
# app/views/components/_card.html.erb
# Later in the partial we can use the method:
Would you like to support Nice Partials development and have your logo featured here? Reach out!
Add to your Gemfile:
gem "nice_partials"bundle exec rake testCopyright (C) 2022 Andrew Culver and Dom Christie . Released under the MIT license.
暂无开放 Issues,或尚未同步最近议题。