7

I load my stylesheets and js files in <head> for performance reasons.

My site has multiple components and each template wants to its own extra header files in inside <% yield(:head).

I tested <% content_for :head do %> .. but then I realize it actually overwrites rather than append to a particular section.

What do you guys use?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
xjq233p_1
  • 7,810
  • 11
  • 61
  • 107
  • have a look http://stackoverflow.com/questions/7512486/ruby-on-rails-yielding-specific-views-in-a-specific-places-in-the-layout – datalost Sep 23 '11 at 03:49
  • @Froyo that is not what I am asking for. Unless I write multiple <% yield but I dun really want to do that – xjq233p_1 Sep 23 '11 at 04:07

1 Answers1

8

content_for actually appends by default. From the documentation, if you were to do...

<% content_for :navigation do %>
  <li><%= link_to 'Home', :action => 'index' %></li>
<% end %>

<%#  Add some other content, or use a different template: %>

<% content_for :navigation do %>
  <li><%= link_to 'Login', :action => 'login' %></li>
<% end %>

If you used...

<ul><%= content_for :navigation %></ul>

It would output...

<ul>
  <li><a href="/">Home</a></li>
  <li><a href="/login">Login</a></li>
</ul>

Just tested this locally on a rails 3.1.0 app to make sure this is still the case and it does this fine.

lloydpick
  • 1,638
  • 1
  • 18
  • 24
  • Here's a [link](https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/capture_helper.rb#L139) to the source which confirm's LLoyd's version. – asymmetric Nov 19 '11 at 00:06