Replies: 4 comments 11 replies
-
render MyComponent.new(arg1: value1, arg2: value2).yield_self do |c|
should_wrap_in_layout ? PartialLayoutComponent.new(arg1: value1, arg2: value2) { render c } : c
end One thing I'll say about the original solution is that yes, it's potentially more readable, but layouts are components in and of themselves, so it's more a convenience of how Action View plays with them than reflective of how things actually work. Another way you could attack this is to have <%# Component template %>
<%= layout do %>
<div>Content</div>
<% end %> And then you'd pass in layout with: render MyComponent.new(arg1: value1, arg2: value2, layout: PartialLayoutComponent.new(arg1: value1, arg2: value2)) |
Beta Was this translation helpful? Give feedback.
-
Maybe part of the fantasy is if Rails could handle ViewCompoents as layouts. This won't work now, but why not:
It would of course require changes to Rails along the lines of what was needed to support ViewComponent in the first place with I'm not sure how realistic that is... but it may be necessary/important for the "make it possible to totally replace normal actionview with viewcomponent" dream... especially becuase you might want to use ViewComponent for top-level controller-level layouts too, which I don't think is possible now? |
Beta Was this translation helpful? Give feedback.
-
In more answering my own question, here's another variation of a helper method: def maybe_wrap_with_component(wrapping_component:, should_wrap:)
if should_wrap
render wrapping_component do
yield
end
else
yield
end
end
<%= maybe_wrap_with_component(wrapping_component: LayoutComponent.new, should_wrap: true) do %>
<%= render MyComponent.new %>
<% end %>
|
Beta Was this translation helpful? Give feedback.
-
I've seen capture used for cases like this. e.g. <% content = capture do %>
my body content here
<% end %>
<% if wrap_with_layout? %>
<%= render MyLayoutComponent.new do %>
<%= content %>
<% end %>
<% else %>
<%= content %>
<% end %> |
Beta Was this translation helpful? Give feedback.
-
In my existing code using standard rails partials, I have a partial rendered sometimes with a partial layout, other times without, based on a condition.
In standard Rails this is fairly elegantly expressed:
We could also imagine
layout: figure_out_layout
, wherefigure_out_layout
can return any of several possible layouts, and/or nil meaning "don't use a layout". But what I'm doing is simply as above.What's the best way to convert this to ViewComponent? The simplest thing I can figure out is this, which isn't very satisfying and involves more duplication and less elegance:
That seems a sad replacement of the elegant readable one-liner.
I can think of a variety of other weird ways to do this, but none great. If we imagined the more complicated case of a method returing one of several possible layouts and/or nil... it gets even trickier.
Does anyone have a better idea? Anything I'm missing?
Beta Was this translation helpful? Give feedback.
All reactions