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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
yieldis how you specify where your content areas is going to go within a layout. You might have something like this:content_foris how you specify which content is going to be rendered into which content area. You might have something like this:The result would be
They are opposite ends of the rendering process, with
yieldspecifying where content goes, andcontent_forspecifying what the actual content is.The best practice is to use
yieldin your layouts, andcontent_forin your views. There is a special second use forcontent_for, where you give it no block and it returns the previously rendered content. This is primarily for use in helper methods whereyieldcannot work. Within your views, the best practice is to stick toyield :my_contentto recall the content, andcontent_for :my_content do...endto render the content.