This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
21. Rendering a view

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
So inside of any controllers that we create, here's what we want to do.
00:03
We want to take the body and write a rendered version of the twig template that we have created. In our case, we've just created home.twig. So we want to say something like this, view and render. We want to choose home.twig and then optionally pass anything down to that.
00:20
And we'll look at that in this episode as well. Now, obviously, at the moment, this isn't going to work because we don't have a render method over on our view class that we created over here. Now, we know that when we set this up in our service provider,
00:33
so over in our view service provider, we created this and passed through the twig instance. So the first thing to do would be to accept this into the constructor. So let's go and create our constructor. We know that this is an environment instance from twig.
00:49
So let's make this protected here and we'll just call this twig. And then we can just really decorate this with any methods that we want. So in our case, we can create render and we can call that whatever you want. So you can change the name of this as well if you want.
01:03
Now, inside of render, we will have a view that we want to render or a template that we want to render. And we'll have an array of data that we want to pass down to that view. And then from twig here, we can just basically call the same method. So we'll say this and twig and render, and we'll pass the view to render
01:24
and we'll pass the data to pass down to that template. So effectively, we're just creating a proxy for this within our own class that we can easily and conveniently access from the container. OK, so now that we've done this, let's head over and give that a refresh.
01:38
And there we go. So now this is our twig template that has been rendered via twig and responded to with HTML. So now what we can do over in home is just do whatever we want in here. So we can even create out a full document in here if we wanted to with a title.
01:56
We'll be looking at changing things like this over later. So we could say no framework and we could just write some stuff to the body like this. And there we go. And we have a full HTML page now to work with rather than manually doing that from our controllers.
02:13
Now, let's keep this simple for now, because we're going to look at templating and extending templates later. Let's output an H1 in here with the name of our app. So how do we do this?
02:24
Well, we just want to pass it down to the data in the render method, which will get passed down to twig. And then we can use this. Now, if you've not worked with twig before,
02:34
the syntax here is going to be a little bit tricky to get started with. But all of this is available in the documentation and we'll be covering pretty much everything you need to know. Okay.
02:43
Let's pass the name of our app down to this view. How do we do that? Well, we use our config up here to read that value. So let's say this config get app and name.
02:56
So now we've passed this array down to our view. We have name as a variable available inside of home.twig. To output a variable, we use two curly braces either side, and then we just output the name of the key that we passed down here.
03:13
So this is just name. So now when we head over, there is the name of our app. So now we can render views. This is a much cleaner way of organizing everything.
03:24
So every time we want to create a new page, we'll create our new route, a new controller, a new view, pass any data down that we want to see in here. And of course, that will be rendered out for us.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!