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

Render behavior in component classes #569

Closed
dvisockas opened this issue Jan 4, 2021 · 5 comments
Closed

Render behavior in component classes #569

dvisockas opened this issue Jan 4, 2021 · 5 comments

Comments

@dvisockas
Copy link
Contributor

Feature request

Would it make sense to have template code in the component class definition file (similar to how JSX works)?

I just thought that it would be cool to have something like a render method that could hold the template code in the class, not a separate file. Something like:

class SomeComponent < ViewComponent::Base
  def render
    <<~ERB
      <h1><%= post.title %></h1>
      <p><%= post.content %></p>
    ERB
  end
end

Motivation

React uses this in JSX. Also this would reduce total component file count by 2. However if someone would like to render js, css, etc. this would need methods like render_js or something (?) (or wouldn't make sense anymore)

Another drawback is the slim/haml indentation, as they would have to be aligned with the left most column of the file, since squiggly heredoc strips whitespace from each line of the string.

@joelhawksley
Copy link
Member

@dvisockas thank you for filing this issue!

The original implementation of ViewComponent actually used almost this exact syntax! We moved to using separate templates for a couple of reasons, including:

  • GitHub doesn't syntax-highlight ERB in HEREDOCs 😬
  • The Ruby team raised some concerns about HEREDOCs and Ruby 3 types.

That being said, I'd love to find a way to go beyond our support for Inline Components. I think that in a lot of cases, having a single file for a component instead of two makes for a cleaner, simpler design.

@dvisockas
Copy link
Contributor Author

It would be possible to write the view part using rails tag helpers, but that would be cruel.
What were the concerns regarding HEREDOCs?

@patchwork-her

This comment has been minimized.

@joelhawksley
Copy link
Member

What were the concerns regarding HEREDOCs?

@dvisockas besides those I already mentioned, perhaps @soutaro can elaborate the concerns re: Ruby 3 typing?

@joelhawksley
Copy link
Member

Closed by #1556

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

No branches or pull requests

3 participants