Skip to content

content_for with flush parameter #4226

Merged
merged 1 commit into from Dec 29, 2011

3 participants

@grentis
grentis commented Dec 28, 2011

Hi all,
I have a problem using content_for with nested layouts. I have a template like this one

...
    <h1><%= content_for :box_title %></h1>
    <div class="box_content">
      <%= yield %>
    </div>
...

called for render multiple partials from a view

...
  <%= render partial: 'agents_box', layout: 'layouts/internal/box' %>
  <%= render partial: 'users_box', layout: 'layouts/internal/box' %>
...

_agents_box.html.erb

<% content_for_clear :box_title do %>Server Management<% end %>

_users_box.html.erb

<% content_for_clear :box_title do %>Users<% end %>

Due to the behavior of the content_for function (concatenate) the page is not displayed correctly. (the first h1 contains "Server Management" and the second one "Server ManagementUsers")

In order to solve this kind of problem I updated the content_for method to accept another parameter (flush - false by default) used to decide if the content should be concatenated or replaced.

Using this in my example the page is displayed in the right way

_agents_box.html.erb

<% content_for :box_title, true do %>Server Management<% end %>

_users_box.html.erb

<% content_for :box_title, true do %>Users<% end %>

Hope this can be useful also for other people

@josevalim
Ruby on Rails member

It looks good, but we probably should do something like this: flush = content if block_given? so we don't always need to pass the second parameter as nil when using the block form.

@sikachu
Ruby on Rails member
sikachu commented Dec 29, 2011

Agree with @josevalim. Would you please fix that and force-push to your branch?

@grentis
grentis commented Dec 29, 2011

I made the changes... I hope I have done the right things with the force-push process

@josevalim josevalim merged commit 5ac0273 into rails:master Dec 29, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.