Should you use content_for
inside a component template?
#1591
-
Hi folks! It feels like an anti-pattern, even though it seems to work, as it couples a component to some external thing. I would appreciate other points of view though as I might be looking at this too narrowly. Cheers! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think you're right on the money here. If you're using <%# app/views/layouts/some_layout.html.erb %>
<%= render NavbarComponent.new %>
<%= yield %> This way, if the data you'd pass to <%# app/views/layouts/some_layout.html.erb %>
<%= render NavbarComponent.new do |c| %>
<% c.user_area(notifications: current_user.notifications) %>
<%= yield %>
<% end %> Said component could even contain the markup for the whole template, and render the page content as its own <%# app/views/layouts/some_layout.html.erb %>
<%= render LayoutComponent.new do %>
<%= yield %>
<% end %> Hope that sparks a few ideas! |
Beta Was this translation helpful? Give feedback.
-
Thanks @boardfish! Cheers! |
Beta Was this translation helpful? Give feedback.
I think you're right on the money here. If you're using
content_for
, then chances are you're doing something at a global level, perhaps involving layouts or needing to pass things around in a way you might find uncomfortable. In the past I've avoided the need forcontent_for
by having layouts render a component:This way, if the data you'd pass to
content_for
changes on a controller basis, you can use a different template that renders the same component with some small tweak – maybe tha…