Layout.erb + yield = stationery for your app

Day 162

Not good stationery or layout

Layout.erb with a yield facilitates a consistent appearance for your app or website in Sinatra.

To break down layout.erb, the first part, “layout”, is the rendering of your code in html, or what your user will see. The “erb” extension allows for the use of embedded Ruby code in your html. A layout.erb file allows you to set up a consistent look from page to page. It’s basically stationery for your application. Without it, to achieve this consistency you would either have to write exactly the same code on each erb file (or copy and paste it from one to the next) for your user to recognize each page of your app.

Yield is the magic that brings the html and erb specific to your view onto the page. It’s the pen that writes the letter that fills the blank page. As long as you have a layout.erb file in your directory, any time you build a route, it will search for the layout file, and fill it in with the view file from your route.

Suppose you create the layout.erb file and forget to put your code in it? Then none of your views will render because the browser looks at your layout.erb file first, and if there’s no code, it looks no further for fill-in information.

Conversely, suppose you build your layout.erb file, but don’t put yield in it or have no view file to fill in the yield? You’ll just get your basic page layout.