So last time we worked really hard to implement this view class that will basically serve as a total
abstraction for the views rendering.
Now it's time that we use this view class inside the controller.
Let's begin with a home controller by creating the first view file.
So views are stored inside the app Views folder.
Let me create a separate folder for every controller.
This time I'm going to call it home because it is for a home controller.
Inside it it's a view for the index action.
So let me call it the same way that would be index.php.
Let me add a div element and maybe let's make it bold just saying hello for now.
So the first test is to see if we can render this template back to the home controller and instead of
returning home, let's use this view.
Class.
First, I add a use statement for the view class, which has the core view namespace, and instead of
returning this text, I use the view only public static method which is render.
Now the template it is home forward slash index.
We skip the extensions and for clarity maybe I can use the named parameters.
So that's the template.
We don't have layout so far.
So I'm going to skip this.
And let me also try data passing.
Maybe I can pass a message here saying hello.
And then in the template.
Let's say that the message is.
And now I should have a variable available here which would have the name message so I can output it.
And now let's try this out.
So when I try this I've got a 500 error.
Looks like this.
Because we don't have nice error handling yet.
So let me see inside the terminal.
What's the problem.
So the template was not found.
I'm looking for the template in apparently the wrong folder.
Okay, I see the problem.
I've used the dear name function.
And this ends up not being able to find the file.
Let's do some debugging.
Then let's use var dump with the path and stop at this point.
So we're going to do some live debugging.
And apparently that's the file.
So we can start from this.
That's the root directory.
And well we don't need to go up in here because we are looking for a file inside this app folder.
That's an easy fix.
Apparently I don't need to go up in here.
So let me fix that in all the places.
Remove the var dump, save the changes and try again.
Okay, so there it is.
We've got this page and that seems to be the message we are passing, which confirms that both the view
rendering and the data passing is working fine.
Next up let's add a main layout layout that we're going to use for all the public pages except a admin
dashboard.
So for layouts let me create another folder inside views and call that layouts, and the main layout
will just be called main.
Like this.
And now let's add some code reuse.
So in the previous app we've got this header.php file.
Why won't we just reuse this as our layout.
So let me just copy this and paste it here.
Now if you don't have this other project open, you can always grab the code for every video.
Under the specific video there are links.
You can just copy and paste that there shouldn't be a problem.
So let's just change this H1 element to my blog and the navigation items.
Well obviously we have different.
So for example we've got link to posts the home page.
And then we don't really chop those files into separate header, footer, etc..
Now inside this main element I can actually output the content.
And also I can add a footer directly inside the layout.
So in the footer let me say that we are copyrighting this.
I'm going to use PHP to output the date the year, which would always be the current one, and say that
we copyright that as my blog.
Now let's close the HTML and body elements.
The other thing I'd like to reuse is the CSS, but instead of putting it all into the style block,
let's add a separate CSS file and let's put it into the public folder because it needs to be available
publicly, call it maybe style CSS and just paste the whole CSS into this file.
Now I'm going to close that and replace this style block with a link element to point to this style
file, which needs a rel attribute called style sheet and decref.
That's just forward slash style CSS, because we are pointing the public folder is the root here, and
obviously the title is not contact form, it is my blog.
Okay, so I think that this is now ready except the content.
So now in every layout we have this special content variable which is all the content rendered from
the individual templates and we need to output it.
So that's just one of the special variables that we have.
This is now ready and we can use it when we render our main view.
So now let me jump to the home controller.
And the only change that I need to make here is to tell which layout I'd like to use.
So let me pass the layout named argument.
And this should be layouts forward slash main.
And that's everything.
This is how our home page looks like.
So in this video we've actually used our view class to render a view and to pass some data to it, which
is available as a variable.
And also we have created a main layout which has some magic variables like the content, for example,
which is all the content rendered from the template so that we can insert it wherever we want.