I’ve wanted more compact syntax for rendering #ViewComponent in #Rails and finally got around to making a proof-of-concept.

Before:
<%= render Admin::HeadingComponent.new(“Hi”) %>

After:
<%= c.heading “Hi” %>

Inspired by the `tag.strong “Hi”` Rails tag proxy.

Curious if others do anything similar!

https://gist.github.com/henrik/d053195e891b08e13c35338a82927f0a

Rails ViewComponent convenience, e.g. `c.heading("Hi")`

Rails ViewComponent convenience, e.g. `c.heading("Hi")` - 0_example.erb

Gist

Found https://ruby.social/@owais/111960149742686423 (@owais) which is rather similar.

That approach seems to predefine helpers that don’t hide the namespace. In this app, I prefer it hid. I think we might also want support for fallback namespaces, e.g. [Admin, AdminAndPublic].

We don’t nest components beyond an Admin/Public namespace, but it makes sense to support, of course!

owaiswiz (@[email protected])

Attached: 1 image I've always felt rendering ViewComponent with Rails is a bit too verbose. Especially if you have namespaces. E.g: render(Admin::Navigation::Item.new(label: 'Example')) I've used a helper to make it nicer on 2 projects so far: vc.admin.navigation.item(label: 'Example') Maybe make it into a gem? (Might seem a bit over-engineered on a first glance. But it's something that might be called 100s-1000s of time on a page, so had to make sure it's fast enough. #rails #ruby #ViewComponent

Ruby.social

@henrik Nice.

Yea, the thing i built by default doesnt hide the namespace.

It does however allow you to customize the behavior to do that if you so wish. I think there are some examples of that in the readme.