content_for block not rendering
itsameandrea opened this issue · 7 comments
Hello!
I'm trying out nice_partials
in a project but I can't seem to get the content_for
block to render within my partial. I've got two files
<!-- views/teachers/_table.html.erb -->
<div class="flex justify-between">
<% if partial.content_for? :filters %>
<%= partial.yield :filters %>
<% else %>
<%= render 'teachers/filters', school: school %>
<% end %>
</div>
<table></table>
<%= render 'teachers/table', school: @school do |partial| %>
<% partial.content_for :filters do %>
<p> hello world </p>
<% end %>
<% end %>
I was expecting <p> hello world </p>
to be rendered within the partial, instead I keep getting the default content. Am I missing something?
Hey, thanks for trying out NicePartials! I think this issue is what #54 fixes.
To verify that, can you try replacing yield
with content_for
, like this and see if it renders properly?
<div class="flex justify-between">
<% if partial.content_for? :filters %>
<%= partial.content_for :filters %> # Replace yield here.
<% else %>
<%= render 'teachers/filters', school: school %>
<% end %>
</div>
Thanks for the quick response @kaspth and for the work on this gem!
That fix doesn't seem to work. To add some more details, I tried adding a <% byebug %>
like so
<div class="flex justify-between">
<% byebug %>
<% if partial.content_for? :filters %>
<%= partial.content_for :filters %>
<% else %>
<%= render 'teachers/filters', school: school %>
<% end %>
</div>
And the result of partial.content_for? :filters
is nil
. So by the looks of it the line to actually inject the :filters
block doesn't even get reached
Hm, strange, I'm a little stumped as to why nothing would be rendering. What version is this?
Just to try something… what happens if you rename the partial
variable to something else in the block? Like this:
<%= render 'teachers/table', school: @school do |cp| %>
<% cp.content_for :filters do %>
<p> hello world </p>
<% end %>
<% end %>
I just remembered some more things we can check. What's your Rails version?
We're depending on some additions in Rails 6.1+ to automatically yield, but we haven't communicated that yet or warned, what happens if you do this?
<!-- views/teachers/_table.html.erb -->
<% yield partial = nice_partial %>
<div class="flex justify-between">
<% if partial.content_for? :filters %>
<%= partial.yield :filters %>
<% else %>
<%= render 'teachers/filters', school: school %>
<% end %>
</div>
<table></table>
Hey @kaspth !
I was having the same kind of issue as @ilrock and it seems it was happening when I would try to access content from yield
.
As an example, starting from the README code :
app/views/components/_card.html.erb
:
<div class="card">
<%= partial.yield :image %>
<div class="card-body">
<h5 class="card-title"><%= title %></h5>
<% if partial.content_for? :body %>
<p class="card-text">
<%= partial.yield :body %>
</p>
<% end %>
<%= yield %>
</div>
</div>
<%= render 'components/card', title: 'Some Title' do |partial| %>
<% partial.content_for :body do %>
BODY
<% end %>
<% partial.content_for :image do %>
IMAGE
<% end %>
<div class="card-footer">
FOOTER
</div>
<% end %>
There, neither partial.yield
nor partial.content_for
would render.
Adding <% yield partial = nice_partial %>
on top of the component view fixes the issue. Yet, you have to call yield
and not partial.yield
to render the block.
Noting that I'm testing all this on Rails 7.
Though, updating the gem to main branch, fixes this issue without the need of <% yield partial = nice_partial %>
and by using partial.yield
instead of yield
.
Got it, yeah. Right now, the automatic yield
is only on the main branch as you found out. I'm closing this for now, since it'll be fixed when we release a new version of Nice Partials proper.
Thank you both for looking into it!