Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content_from to let partials relay contents #53

Merged
merged 52 commits into from
Sep 5, 2022

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Aug 21, 2022

Fixes #11

Sometimes there's a partial that'd like the named contents of another.

content_from lets a partial sift the content, and pass a hash to rename sections,
from the outer partial:

<% partial.content_for :title, "Hello there" %>
<% partial.content_for :byline, "Somebody" %>

<%= render "shared/title" do |cp| %>
  <% cp.content_from partial, :title, byline: :name %>
<% end %>

Sprung out of the discussion in #38, though I didn't feel the need to add parent tracking and flipped the call order, so the inner partial extracts from the outer and that feels clearer what context we're currently in.

@kaspth

This comment was marked as resolved.

lib/nice_partials/partial.rb Outdated Show resolved Hide resolved
@kaspth kaspth force-pushed the relay-partial-contents-with-extract-from branch 2 times, most recently from 5570e73 to 2c32681 Compare August 22, 2022 17:25
@kaspth kaspth changed the title Add extract_from to let partials relay contents Add content_from to let partials relay contents Aug 22, 2022
@kaspth kaspth force-pushed the relay-partial-contents-with-extract-from branch from 2c32681 to b6e25c2 Compare August 22, 2022 17:26
@kaspth
Copy link
Contributor Author

kaspth commented Aug 22, 2022

Ok, got my naming concerns resolved and went with cp.content_from partial, :title, byline: :name.

@kaspth kaspth added this to the v1.0.0 milestone Aug 29, 2022
If we're starting to layer on more complex behavior to Section and
exposing them, it's nice to have an alternate implementation that's
easier to maintain for our content_for inter-op with Rails.
It's possible we may want a general method_missing hook that delegates
to @view_context and concatenates the result and returns self
for chaining. Would allow for something like this too:

```ruby
<% partial.title.link_to(@document.name, @document) %>
```
This lets you generate different elements with overrides without appending
to the content/options for the next invocation.

Additionally, `tag#method_missing` responds to everything so we'll never
reach super.
Various explicit nil returns were inserted to prevent things like

<%= partial.title.t ".title" %> # <% %> should have been used here.

But the more extensions we add the more we have to be careful of it,
and it prevents other potentially interesting directions like chaining.
…and then we can expose `write`, with some potential for chaining.
Removes need for the old test task in the Rakefile.
When we fixed the view context stubbing, `capture` didn't just `yield`
anymore but also processed the returned value as a string
https://github.com/rails/rails/blob/06e9fbd954ab113108a7982357553fdef285bff1/actionview/lib/action_view/helpers/capture_helper.rb#L47

So our expectation that `partial.body { NameComponent.new(:plain) }` works,
won't work in practice.

But `partial.body { render NameComponent.new(:plain) }` is what's needed.
Sometimes there's a partial that'd like the named contents of another.

`content_from` lets a partial sift the content, and pass a hash to rename sections,
from the outer partial:

```erb
<% partial.title "Hello there" %>
<% partial.byline "Somebody" %>

<%= render "shared/title" do |cp| %>
  <% cp.content_from partial, :title, byline: :name %>
<% end %>
```
@kaspth kaspth force-pushed the relay-partial-contents-with-extract-from branch from b6e25c2 to 051647a Compare September 5, 2022 07:43
@andrewculver andrewculver merged commit 693a58c into main Sep 5, 2022
kaspth added a commit that referenced this pull request Sep 5, 2022
* main:
  Introduce Capybara to the test suite (#55)
  Add `content_from` to let partials relay contents (#53)
@kaspth kaspth deleted the relay-partial-contents-with-extract-from branch September 5, 2022 16:59
@kaspth kaspth mentioned this pull request Sep 5, 2022
kaspth added a commit that referenced this pull request Sep 6, 2022
* main:
  Ignore /tmp if you're running BT in tmp/starter like CI does
  Fix some CHANGELOG issues and refit some examples + words
  Skip needless CI steps (#60)
  We don't need to depend on sqlite3 that was for an earlier version of the ViewComponent integration
  Fix #54 to use new section yield syntax
  Mixing `yield` call styles (#54)
  Expose Sections as an alternative to `content_for` (#57)
  Introduce Capybara to the test suite (#55)
  Add `content_from` to let partials relay contents (#53)
  v0.1.9
  v0.1.8
  Document release 🎉 (#52)
  Rename `Partial#output_buffer` to `Partial#yield` (#41)
  Allow outer partial access when capturing (#49)
  Fix accessing `partial` before rendering leaks state (#47)
  Use load hooks for monkey_patch loading (#48)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partials should have an easier time delegating to other partials.
2 participants