Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:9Length:41 minutes
Laravel400
  • Overview
  • Transcript

2.2 Static Pages

Most websites consist of several pages which may be static. This means the content on them is written directly into the view and it does not change often. In this lesson we'll look at how we can use our template to create new static pages.

2.2 Static Pages

We've got our template all set up and ready to go, well almost, there is one thing that we still need to do and that's to make this template a proper template and by that I mean to make it extendable. To do this we're going to be using one of blade's handy functions, the one we want is called yield. This function lets us inject the content of another blade view into our template file. For this to work we're also going to make use of blade's extend function. Sound confusing? It's okay, as it is a little confusing. So how about we step through it and see it in action. What we'll do is create a home.blade.php file alongside our template. In this file you need to use the extends function on the very first line. The argument that we pass to this function is the name of the view that we wish to extend. So in our case this is going to be template. So we're now extending our template. The next part is to actually inject some content within the content area of our template. To do this in our home view, we first define the content that we wish to inject. But we need to define it within a section. Blade lets us create as many sections as we need, and we define them by giving them a name. These sections let us inject content elsewhere within other views. So, we're going to give this section a name of content. And we can end the section by simply calling the stop function. Now everything within the section and the stop functions are considered to be part of the content section, and we can put whatever we want in here. For now, let's just put in some dummy lorem ipsum text. Finally, we need to display this section within our template. Now do you remember the yield function that I mentioned earlier? Well this is where it's used. We used yields to display sections. So in our case we want to yield the content section within the content area of our template. So now within our web routes, instead of returning in the template view we can simply return the home view. Now remember, this view extends the template, in that it defines a content section which is being yielded by our template. So now, in our browser, if we refresh we'll see that the lorem ipsum dummy content has been injected into our template. We've now built our first starting page, and it's our homepage. So how about we now put together an about page using the exact same method. First we're going to create the view file and we'll name it about.blade.php. Now remember our view files need to extend our template so we call at extends and we pass in a template as the view that we wish to extend. To inject content within the template we need to create the content section. For our about page, we'll again enter some more dummy lorem ipsum text, if you'd like, you can put in some real content, cuz remember, this is your website and now in our web routes file, we can wire up the new route by simply copying our home route and then changing the path from a forward slash to about. We're also going to need to change the view that it's returning so that our about view is returned instead of the home view. Now I've run our web browser, if we hit this about route, we can see that a new lorem ipsum text is being displayed. So we've now got two static pages set up. In the next lesson we are going to take a look at how we can link pages together more easily with named routes. [BLANK AUDIO]

Back to the top