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

content_for block not rendering #58

Closed
itsameandrea opened this issue Sep 2, 2022 · 7 comments
Closed

content_for block not rendering #58

itsameandrea opened this issue Sep 2, 2022 · 7 comments

Comments

@itsameandrea
Copy link

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?

@kaspth
Copy link
Contributor

kaspth commented Sep 2, 2022

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>

@itsameandrea
Copy link
Author

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

@kaspth
Copy link
Contributor

kaspth commented Sep 2, 2022

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 %>

@kaspth
Copy link
Contributor

kaspth commented Sep 5, 2022

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>

@kaspth
Copy link
Contributor

kaspth commented Sep 12, 2022

@ilrock hey, is this still an issue?

@acekat
Copy link

acekat commented Oct 20, 2022

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.

@kaspth
Copy link
Contributor

kaspth commented Nov 7, 2022

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!

@kaspth kaspth closed this as completed Nov 7, 2022
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