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

Auto-capture render blocks and expose context specific _p #35

Closed
wants to merge 10 commits into from

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Jul 2, 2022

Ref: #32

This attempts expose a context specific _p, so the below would work and point to the right _p instance:

# _card.html.erb
_p.content_for :title

# show.html.erb
render "card" do
  _p.content_for :title, t(".title")
end

To do this, we're attempting to always auto-capture a block that's passed to render like the render "card" do case, e.g. the card partial doesn't have to insert yield _p.

kaspth and others added 10 commits July 1, 2022 19:57
In a template like this, we now infer that the block takes 1 parameter
and automatically yield a `np` composable content section:

```ruby
render "card" do |card|
  card.yield :title, t(".title")
end
```

The rendered "card" partial would previously need to be annotated with
`yield content = np` but that's no longer needed, and it can instead just
access the yielded Partial instance that's exposed in `content`:

```
content.content_for :title
```

Note: it was custom to do `yield p = np` previously, but I've chosen `content`
as slightly more descriptive now that we're automatic.

This doesn't break compatibility, but does perform an extra yield until
apps switch. We could put this behind a config flag.
Ruby's numbered parameters lets you access via _<index> like this:

```ruby
render "card" do
  _1.yield :title, t(".title")
end
```

So as an experiment, I'm adding a `_p` alias to access the nice partial
instance that plays on Ruby's numbered parameters convention.

Here's how the card partial would use it:

```html+erb
<%= _p.content_for :title %>
```
@kaspth
Copy link
Contributor Author

kaspth commented Jul 2, 2022

There's a bug where we're capturing twice… and it's not easily solvable 🤪

Base automatically changed from auto-yielding-based-on-block-arity to main July 3, 2022 03:18
@kaspth
Copy link
Contributor Author

kaspth commented Jul 4, 2022

There's some interesting stuff in here, but I have some other ideas for changing the auto-capturing which I want to explore.

@kaspth kaspth closed this Jul 4, 2022
@kaspth kaspth deleted the auto-capture-render-blocks branch July 4, 2022 13:34
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.

None yet

2 participants