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 Phlex::HTML#render for rendering a component into a String #429

Merged

Conversation

marcoroth
Copy link
Contributor

@marcoroth marcoroth commented Jan 10, 2023

I'm not sure if this was left out on purpose, but I feel like having a static render method shorthand for Phlex::HTML to render the component into a string reads a lot nicer compared to having to instantiate the class and calling call after, especially when working with blocks.

For example, if you are are using Phlex components for Turbo Stream/CableReady payloads:

respond_to do |format|
  format.turbo_stream { 
    render turbo_stream: turbo_stream.replace("#posts", Views::Posts::Index.new(posts: Post.all) { ... }.call ) 
  } 
end

vs.

respond_to do |format|
  format.turbo_stream {
    render turbo_stream: turbo_stream.replace("#posts", Views::Posts::Index.render(posts: Post.all) { ... }) 
  } 
end

Let me know if you feel like this is a valuable addition.

@joeldrapper
Copy link
Collaborator

joeldrapper commented Jan 11, 2023

Looking at your specific example, I think the turbo_stream.replace method should accept a renderable as its argument.

That aside, I can definitely see value in having a render class method on Phlex views. I like that name, but I think it should possibly be an alias for a call class method so Phlex views classes can be callable.

lib/phlex/html.rb Outdated Show resolved Hide resolved
@marcoroth marcoroth force-pushed the static-render-method-on-phlex-html branch from 479f8a7 to c08c817 Compare January 12, 2023 11:26
@marcoroth marcoroth marked this pull request as ready for review January 12, 2023 11:26
@marcoroth marcoroth force-pushed the static-render-method-on-phlex-html branch from c08c817 to 1ca3374 Compare January 12, 2023 11:27
@joeldrapper joeldrapper merged commit 753245d into phlex-ruby:main Jan 12, 2023
@marcoroth marcoroth deleted the static-render-method-on-phlex-html branch January 12, 2023 11:47
@Bumppoman
Copy link

This is going to be very useful when I tackle integrating my Phlex views with dry-system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants