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