Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Web Apps with Sinatra Finishing Touches Layouts

Michael Walker
Michael Walker
598 Points

Can someone describe what <%= yield %>

Specifically, when it is included in the body of the layout.erb file in the <body> tags? Thanks.

1 Answer

Mike Gabriel
Mike Gabriel
8,402 Points

The official documentation describes yield as Within the context of a layout, yield identifies a section where content from the view should be inserted.

In every default Rails project your main layout file is application.html.erb. Unless otherwise specified, all your views will yield to this layout file.

Rather than repeating all of the meta tags and stylesheets over and over for every view, because this data generally stays static throughout the website, we only set it up once in the application.html.erb file and all of our controller views yield to it.

Have a look at the official documentation section for more information - http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield