Explain what a yield statement in layout.erb does and why we use it
Implement a yield statement in layout.erb
Layout
If you look at pretty much every website, you'll notice that there are things that exist across all of the site's pages. Typically, the navigation bar and the footer content stay the same. There may also be menu options that stay consistent across all pages.
You could copy and paste the HTML and ERB for nav bar and make sure that code is in every single erb file, but that isn't at all DRY.
In order to not repeat ourselves, we can create a single file, layout.erb, that contains all of the code we want to exist on every single web page.
Below is the HTML for a website that has a header and links to JavaScript files.
We want every page to have a <head> tag with links to Bootstrap's and our own CSS files. The body of our site contains the heading I love cats and a cat gif. At the bottom, we have our jQuery and Bootstrap links.
Now, let's say we have an index.erb with the following code:
When the above controller action is triggered and the erb method is called, it looks to see if there is a view titled layout.erb. If that file exists, it loads that content around the desired erb file, in this case index.erb.