content_for: Multiple yields in the same layout

by woody2shoes on June 4, 2009

Have you ever wished you could call yield multiple times in the same layout in order to get extra stylesheets, javascript, or a dynamic sidebar? Having worked on several projects that needed this soft of functionality, I’ve probably built this in three or four times. Then I started watching railscasts.

There’s a method called content_for that provides this functionality. All you have to do is call content for in your view. Here’s the erb markup:


Then, in your layout all you have to do is call yield like you normally would in your layout with on small difference. Here’s the erb markup.


I picked up this tip from Ryan Bates Railscast Episode #8

  • a_

    trolls not dead, yeah

Previous post:

Next post: