17

For example: content_for(:stuff) vs yield :stuff

I know they are implemented slightly differently, but is there any real functionality difference?

Is there a generally accepted best practice?

4 Answers 4

49

yield is how you specify where your content areas is going to go within a layout. You might have something like this:

<div>
  <h1> This is the wrapper!</h1>
  <%= yield :my_content %>
</div>

content_for is how you specify which content is going to be rendered into which content area. You might have something like this:

<% content_for :my_content do %>
  This is the content.
<% end %>

The result would be

<div>
  <h1> This is the wrapper!</h1>
  This is the content.
</div>

They are opposite ends of the rendering process, with yield specifying where content goes, and content_for specifying what the actual content is.

Is there a generally accepted best practice?

The best practice is to use yield in your layouts, and content_for in your views. There is a special second use for content_for, where you give it no block and it returns the previously rendered content. This is primarily for use in helper methods where yield cannot work. Within your views, the best practice is to stick to yield :my_content to recall the content, and content_for :my_content do...end to render the content.

3

yield:

yield identifies a section where content from the view should be inserted

content_for:

The content_for method allows you to insert content into a named yield block in your layout. For example, this view would work with the layout that you just saw:

yield :stuff will grab the contents that are pushed by content_for(:stuff)

So, using yield you can define the sections in your view/layouts and you use content_for for adding contents to those sections. Any unnamed yield will grab all other contents.

You can learn more about it reading the tutorial.

3
  • But wait, this also works for me, in the model view, I have content_for :stuff, "the stuff" and in the layout view I have if content_for?(:stuff) content_for(:stuff) ... bypassing the need for 'yield'...
    – Bryan
    Oct 31, 2012 at 3:56
  • this does not make any sense. it should be like if content_for?(:stuff) yield(:stuff). It means that, if :stuff has something to yield/produce, you produce it. This is needed when your parent will render something different based on the yield Oct 31, 2012 at 4:00
  • 5
    No, if content_for is invoked with no block, it returns the previously rendered content, if any.
    – user229044
    Oct 31, 2012 at 4:03
2

Calling #content_for stores a block of markup in an identifier for later use. In order to access this stored content in other templates, helper modules or the layout, you would pass the identifier as an argument to content_for. yield can still be used to retrieve the stored content, but calling yield doesn't work in helper modules, while content_for does....more: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

2

From the Ruby on Rails API:

Calling content_for stores a block of markup in an identifier for later use. In order to access this stored content in other templates, helper modules or the layout, you would pass the identifier as an argument to content_for.

Note: yield can still be used to retrieve the stored content, but calling yield doesn't work in helper modules, while content_for does.

You can then use content_for :not_authorized anywhere in your templates.

<%= content_for :not_authorized if current_user.nil? %>

This is equivalent to:

<%= yield :not_authorized if current_user.nil? %>

content_for, however, can also be used in helper modules.

https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.