Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Our site has many different ERB templates, each containing HTML code that should go in the middle of a document. But the code at the start and end of each page will always be the same. It would be a shame to have to replicate the same HTML head elements and CSS stylesheet link across every ERB template file in our app. That's why Sinatra offers a better way. We can move the shared outer wrapper code to a single ERB file, called a layout. Then we can embed any of the templates we need within the layout. We get the same wrapper code on every page of our site, but we only need one copy of the code.
-
0:00
We just set up our welcome.ERB template as a proper HTML document.
-
0:05
And added a reference to its CSS stylesheet.
-
0:08
As a result, our welcome page looks much more stylish but
-
0:11
our other templates are still unstyled.
-
0:15
It looks like we'll have to make those same changes in our other templates.
-
0:19
Or will we?
-
0:21
Our site has many different ERB templates,
-
0:23
each containing HTML code that should go in the middle of a document.
-
0:27
But the code at the start and end of a page will always be the same.
-
0:31
It would be a shame to have to replicate the same HTML head elements and
-
0:34
CSS stylesheet link across every ERB template file in or out.
-
0:39
That's why Sinatra offers a better way.
-
0:40
We can move the shared [INAUDIBLE] code to a single ERB file called a layout.
-
0:45
Then we can embed any of the templates we need within the layout.
-
0:48
We get the same wrapper code on every page of our site.
-
0:51
But we only need one copy of the code.
-
0:54
Let's move our new header code to a layout file so
-
0:56
we can have CSS styling on every page of our app.
-
1:00
A Sinatra layout is really just another erb template.
-
1:03
The only difference is in the way it's used layout files reside in the views
-
1:07
directory with all the others and they have a .erb extension like all the others.
-
1:11
So we'll just create a new file within views called page.erb.
-
1:20
Then we'll go into our welcome.erb file and cut the heading out.
-
1:28
Then we'll go back to welcome.erb and cut out the footer text and
-
1:33
paste that into page.erb as well, making sure that it's indented consistently.
-
1:43
Since there's no longer code surrounding our welcome.erb template we should
-
1:47
probably go back and un-indent the code there.
-
1:50
Then we can safely close the welcome template.
-
1:53
Now we need to indicate where other templates should be nested within
-
1:56
our layout.
-
1:57
For this app we're going to want our templates inserted within the HTML
-
2:00
body element.
-
2:01
Because all our templates make up the body of their respective HTML documents.
-
2:05
So we'll insert a line between the body and
-
2:07
closed body tags and add an ERB output tag.
-
2:13
Within that tag we're going to use one keyword, yield.
-
2:17
This is the same keyword that sees within a Ruby method to invoke a block.
-
2:21
Here it's invoking whatever ERB template is being nested within this one.
-
2:25
Finally anywhere in our app that we render an ERB template we need to specify that
-
2:29
our layout should be used.
-
2:32
The ERB method takes an optional second argument with a hash of options.
-
2:40
If you specify a key of layout and the name of
-
2:44
the layout file is a symbol, it would look for
-
2:47
a template with that name in your views directory and treat it as a layout.
-
2:51
So if we specify layout with the symbol of page,
-
2:55
it'll load page study ERB from our view sub directory.
-
2:59
Whatever template you've specified in the call to ERB
-
3:02
will be ramped within the layout you specified.
-
3:05
Let's use our page.ERB layout on the welcome and new templates for starters.
-
3:11
We'll restart our server.
-
3:16
And reload the root path.
-
3:17
There is no change.
-
3:19
So you can see that our CSS styling is still applied.
-
3:22
Even though we remove that header from the Welcome.erb template.
-
3:26
Now let's click the button to go to the new page form.
-
3:29
And you can see the style sheet is being applied here too.
-
3:33
If we view the HTML source for
-
3:35
that page, you can see the HTML from our new .ERB template.
-
3:43
And if we expand the head element, we'll see the link to the style sheet.
-
3:49
That comes from the page .ERB layout
-
3:51
our template code is being embedded within the layout code.
-
3:55
Now we could go through the rest of our app finding all the calls to the ERB
-
3:58
method and specify that they should use the Is the page .ERB layout.
-
4:04
But since we only have one layout for our entire app,
-
4:06
we may as well make it the default.
-
4:08
That way, it'll automatically be applied around every template,
-
4:11
even if we don't specify a layout.
-
4:14
An apps default layout is loaded from a file named layout.ERB.
-
4:18
So we can just go to our page .ERB file and rename it to layout .ERB.
-
4:29
Then we can go back to our app code and remove the references to the page layout.
-
4:39
Layout .ERB should now be used by default.
-
4:42
Every time we were under an ERB template on our app.
-
4:45
Let's restart the server.
-
4:51
And give it a try.
-
4:52
We can visit the welcome page in the new wiki page form.
-
4:58
And CSS styling will still be applied.
-
5:01
We'll now also see styling when viewing a page or when editing it.
-
5:06
Any template we render will still get the headers from our layout above
-
5:10
the template code.
You need to sign up for Treehouse in order to download course files.
Sign up