phlex-ruby / phlex

A framework for building object-oriented views in Ruby.
https://beta.phlex.fun
MIT License
1.28k stars 83 forks source link

Rendering Phlex Views like Rails partials #754

Closed reeganviljoen closed 2 months ago

reeganviljoen commented 2 months ago

It would be amazing for if Phlex Views could be rendered like partials

e.g instead of:

render Nav.new

being able to call the Phlex view like a partial

render 'nav'

The main benefit here is it would make this amazing library even more accessible to the larger Ruby on Rails community

reeganviljoen commented 2 months ago

@joeldrapper I was inspired by your collaboration in the larger Rails community, so I wrote a gem(still pre-release) off the back of @joelhawksley pr into Rails

I hope this can be a helpful starting point to help Phlex be more accessible to the wider community

joeldrapper commented 2 months ago

Hey, thanks for opening the issue. Couple of initial thoughts:

Firstly, I don’t think we should support this style render 'nav' for a few reasons:

That said, I am interested in supporting something like render @post where there is some kind of mapping from the Post object to a component that can render that post object. I think that's more what the view component discussion was about unless I’ve missed something.

joeldrapper commented 2 months ago

One additional thought: Phlex’s own render method is meant to be as flexible as possible. render "nav" in Phlex (not phlex-rails) actually renders the literal text "nav". This is useful polymorphism because it allows you to create components that accept arguments that can either be provided as strings or other renderables.

If I have a component like this:

class Box < Phlex::HTML
  def initialize(content:)
    @content = content
  end

  def view_template
    div(class: "box") { render @content }
  end
end

I can render it with a component:

render Box.new(
  content: SomeOtherComponent.new
)

or a string

render Box.new(
  content: "Literally a string"
)

It also works with an enumerable of any renderable.

In phlex-rails we're hoping to support rendering strings like this and allowing you to render partials explicitly with the partial: keyword argument. See this PR https://github.com/phlex-ruby/phlex-rails/pull/149 and associated Rails issue that has so far held up this PR https://github.com/rails/rails/issues/51015

thedumbtechguy commented 2 months ago

That said, I am interested in supporting something like render @post where there is some kind of mapping from the Post object to a component that can render that post object. I think that's more what the view component discussion was about unless I’ve missed something.

You could do that by registering a template engine e.g. https://github.com/slim-template/slim-rails/blob/master/lib/slim-rails/register_engine.rb

reeganviljoen commented 2 months ago

You could do that by registering a template engine e.g. https://github.com/slim-template/slim-rails/blob/master/lib/slim-rails/register_engine.rb

@thedumbtechguy Not really, it let's you render a different template, but how do you translate @posts to PostView

That said, I am interested in supporting something like render @post where there is some kind of mapping from the Post object to a component that can render that post object. I think that's more what the view component discussion was about unless I’ve missed something

@joeldrapper I will add this functionality this weekend, I am also going to put all the other render 'modes' behind a config so you can choose what you want to use, does that sound good?

joeldrapper commented 2 months ago

I will add this functionality this weekend, I am also going to put all the other render 'modes' behind a config so you can choose what you want to use, does that sound good?

Being able to use render @post is something we should develop carefully with the view component team, probably sharing the same common gem and implementing the same interfaces.

I don’t know what render modes you mean. I’m not keen on introducing configuration for render modes. Instead, we have a flexible render method that can render lots of different things.

joeldrapper commented 2 months ago

Closing this as I don’t think any action is needed. We should probably open a new issue in the phlex-rails repo to address implicit rendering of resources (render @posts and render @post).