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
Using the Twig template language we are able to create a single file, our layout, that will form the framework for our HTML. We can then use the extends command to add new content or update existing content inside our layout.
.twig is the file extension for a twig file
Twig Layouts with Extends
Twig Blocks
-
0:00
Well, the first thing we're gonna wanna do is actually create a layout for
-
0:03
our project.
-
0:04
That way we can eliminate this repeated code.
-
0:07
The way we're gonna do that is by using the extends function or
-
0:11
method that is given to us by twig.
-
0:13
Let's head back over to our index.php file and work that out.
-
0:18
So, now, our templates here are index.html and contact.html.
-
0:24
I'm gonna create a new file.
-
0:25
This new file is gonna be my main layout.
-
0:28
So, I'm gonna type out main.twig as the extension to tell
-
0:32
the interpreter we're gonna use twig to interpret this and compile it.
-
0:37
Now, if I open up my twig file, I'm gonna put in the code here
-
0:42
from my index.html file, so this.
-
0:46
I wanna use everything I can here without repeating it in the next file.
-
0:50
So, I'm gonna go ahead and grab everything here, and I'm gonna copy it.
-
0:56
Then, I'm gonna head back over to my twig file and set up my main template.
-
1:02
Let's start by pasting in the code from the index.html file.
-
1:07
Okay. That's a lot of code.
-
1:08
I'm gonna hit save on that and go back up to the top.
-
1:12
Now, I can look at a few things here that I know are gonna be repeated.
-
1:15
Something that I might wanna add something to, something that I might wanna modify.
-
1:20
I'm gonna do that by using content blocks.
-
1:23
So, what I'm gonna do first is inside of my head tag here,
-
1:27
I'm gonna create a tag using the twig syntax to a block for the head.
-
1:34
So, I'm gonna start with two curly braces and then,
-
1:37
inside of those, I'm gonna do two percent signs.
-
1:39
I'm gonna open up some space in there, give it a little wiggle room.
-
1:43
Then, I'm gonna type the keyword block and then I'm gonna type head.
-
1:47
This is just an identifier to tell me what block I'm gonna actually be modifying.
-
1:52
Then, I'm gonna tab everything that I want in there in one.
-
1:56
Then, I'm going to close this out by the same way with two curly braces and
-
2:01
two percentage signs.
-
2:03
Except inside of this, I'm gonna type endblock.
-
2:06
Now, you can leave it just like this, but I like to give it a little bit
-
2:10
more clarity, and instead of just endblock, I'll do endblock head.
-
2:15
That tells me it's ending that particular block.
-
2:18
Just good for readability.
-
2:20
I'm going to save that and that's my first step.
-
2:22
That means that I can modify that block as much or as little as I want
-
2:28
in any file that extends this main template or main twig file.
-
2:33
What other sections could we do this for?
-
2:35
Okay, we're gonna definitely gonna wanna do it for that flash feedback there, so
-
2:39
the success message which is commented out.
-
2:42
We'll get to that a little bit later.
-
2:44
Now, if we scroll down, you'll see we have our header.
-
2:48
Well, I actually want to take the header and
-
2:49
move it to a completely different file.
-
2:51
Do that too.
-
2:52
Let's keep going down, see if we can add anymore blocks.
-
2:56
Actually, I can add one up here.
-
2:58
The title might want to change.
-
3:00
So, let's do the same thing we've done here, where we did block head.
-
3:03
I'm just going to copy it and paste it inside of my tab.
-
3:06
Where these go, doesn't matter.
-
3:08
But this is not the head, this is the title.
-
3:11
All right, there's our title.
-
3:12
Now, I wanna end that block too, so I'm gonna copy this again.
-
3:17
And at the very end of the content, I'm gonna type endblock and title.
-
3:23
That way I can change the title as much as I want on any page
-
3:26
that implements the main template file.
-
3:29
Anymore that we'd wanna do?
-
3:31
Let's go down a little bit.
-
3:33
Let's say we might want to change this hero image here.
-
3:36
We like the tag, the div is fine.
-
3:38
But what we can do is do a block here and we'll call this block, hero,
-
3:42
for our hero image.
-
3:44
And then, we'll paste it in at the end and we'll say endblock.
-
3:49
And make sure you change it to hero.
-
3:53
So, now, you know that anything inside of this div here, the tag, the image,
-
3:59
whatever you want can be changed by simply adding to the block hero.
-
4:03
All right, what else do we wanna do?
-
4:05
Well, I'm gonna go ahead and scroll down a little bit more, and
-
4:08
see if there's another block that I might wanna change.
-
4:11
Well, for sure the footer is something I'm gonna wanna do, right?
-
4:14
So, let's do a block footer.
-
4:15
That's one of the things that we can paste in.
-
4:18
So, I'll come over and add block footer.
-
4:20
And we're gonna do that inside of our footer tag,
-
4:23
that way it doesn't mess up our styles.
-
4:25
Okay, we're going to tab it in.
-
4:26
It's not necessary but definitely good for clean up.
-
4:29
And then, do our end block.
-
4:31
All right.
-
4:32
The last bit, the big bit, the one that matters the most, is our content.
-
4:37
This main area here that has all of this content,
-
4:40
I'm just gonna copy it all out, and delete it.
-
4:44
Now, inside of here, then I'm going to create another content block, or
-
4:48
another block and call it content.
-
4:51
And it's blank by default.
-
4:53
That's fine, cuz we're always gonna want content on every page.
-
4:57
So, I'm gonna end this block.
-
5:00
And that's gonna be the start of our template.
-
5:03
The next thing we're gonna need to do is actually create a template for
-
5:07
each of the pages that we're gonna serve up, such as index and contact.
-
5:12
I'm gonna do that by simply renaming the index.html file.
-
5:17
I'm gonna rename it index.twig.
-
5:21
Okay? In fact,
-
5:22
I'm gonna rename it a little bit more than that because it's not just an index file,
-
5:26
it's the about file.
-
5:27
So, rename, about.
-
5:32
Okay, now the next step here is the part that gets really interesting and
-
5:36
super cleans up our code.
-
5:39
Let's go here now and get rid of everything except for our content.
-
5:44
So, let's go here, we're gonna get rid of all of this.
-
5:49
We're gonna get rid of all of the header.
-
5:50
We don't need any of that.
-
5:54
We're gonna leave nothing but the content.
-
6:00
All right, at the very bottom, same thing.
-
6:02
Get rid of our main tag and everything here.
-
6:04
Now, I'm gonna save this, and this is not gonna work.
-
6:08
We have to do a couple of special commands at the top to make sure we know what
-
6:13
file we're trying to fix or what file we're trying to include.
-
6:16
We're gonna say with using a twig template command that we wanna extend
-
6:21
our main template.
-
6:23
So, we do that by our two curly braces, our two percentage signs,
-
6:27
and then a couple of spaces just for clarity.
-
6:30
Then, we're gonna type in the keyword, extends.
-
6:34
And then, in a string format, the file that we actually want to extend.
-
6:38
So, that's going to be main.twig.
-
6:42
Okay. Now, that's gonna get our header and
-
6:44
footer but its just going to throw this content anywhere on the page.
-
6:47
We actually need to put this inside of a content block and
-
6:51
the way we do that is the same way we did it here in the main.twig.
-
6:55
So, it says block content.
-
6:57
We're gonna do block content.
-
6:59
I'm gonna copy that over.
-
7:01
Same thing, block content.
-
7:03
Now, the only way it knows that this is content that's serving
-
7:07
into a file is that we're using extends at the very top.
-
7:11
I'm gonna actually grab all of this that we have from all the way here from
-
7:14
the bottom, gonna tab it in.
-
7:17
And then, finish our content block by ending it with endblock.
-
7:22
Okay. Save that file and
-
7:23
that should do it for us.
-
7:25
Let's go ahead and see if this works.
-
7:28
Just by going back to our index.php file.
-
7:33
So, we'll hit preview and then, we have a slim application loader error.
-
7:37
Okay.
-
7:38
It says, we're unable to find the template index.html.
-
7:41
Okay. There is a reason for
-
7:42
that we no longer have a template index.html.
-
7:46
We only have a main.twig and an about.twig.
-
7:50
Well for us, we're not gonna serve main.twig.
-
7:53
We're gonna serve our about page.
-
7:55
So here, we're using our new views.
-
7:58
We're going to render not just the index, but the about page.
-
8:03
So, about.twig.
-
8:06
Now, we go back and refresh and hopefully that works for us.
-
8:09
Let's head over and hit refresh.
-
8:13
There we go, Ralph Waldo Emerson and it's our about page.
-
8:16
We've done this all by extending a big template.
-
8:20
So if you look at our index.php We've just simply said about.twig and
-
8:25
then for our contact page, we're gonna do the same, so
-
8:28
contact.twig.
-
8:34
Okay, now using the same thing that we did here, on line 21 to 23,
-
8:40
rendering our about.twig, I want you to take the time to do the Contact.html and
-
8:47
make it contact.twig and I'll meet you in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up