Closed reeganviljoen closed 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
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:
render Nav.new
(and if you don’t need to pass any arguments, render Nav
)."nav"
map to? We would probably need to take the current module, split it at "::"
and walk up looking for matches. In the end, this would be subtly different to Ruby’s own relative constant lookup, which is lexical.Phlex::Kit
there's also another style that is even simpler than render Nav do
, render Nav.new do
or render "nav" do
. With Phlex::Kits, you can do just Nav do
. See details of this experimental feature here. https://github.com/phlex-ruby/phlex/releases/tag/1.10.0That 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.
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
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
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?
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.
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
).
It would be amazing for if Phlex Views could be rendered like partials
e.g instead of:
being able to call the Phlex view like a partial
The main benefit here is it would make this amazing library even more accessible to the larger Ruby on Rails community