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.

Episode summary

In this episode, we dive into rendering views using Twig in our controllers. We start by hooking up our controller so that, instead of sending boring plain text, it takes advantage of our newly created home.twig template. You'll see how to use a render method (which we implement ourselves) to output the view, and how to pass data down to it.

We then walk through setting up the render method on our View class, making sure Twig's environment is set up in the constructor, and using it as a little proxy so we can easily call Twig from anywhere in our app. Once that's done, we give our page a refresh and—boom—the Twig template is rendering our HTML.

After that's working, we show how easy it is to update the template, like putting a full HTML document in place, changing the title, and generally moving all the markup out of our controllers and into Twig files. We also cover the basics of passing data from our controller down into the template (like the app name), and how to get at that data in Twig using its syntax. If you're brand new to Twig, don't worry—we promise to walk through the basics as we go.

By the end, you've got a much tidier way to render your pages: one controller, one view, and a really clean hand-off of data between them. You'll be all set up to pass whatever info you want down to your views in the future!

Episode discussion

No comments, yet. Be the first!