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
22. Debugging in Twig

Episodes

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

Transcript

00:00
While you're working inside of Twig templates, there's a good chance that you'll want to dump stuff out to the page, debug stuff, and that's what we're going to cover now. Now, remember a little bit earlier over in our view service provider, we just set debug to true. Now, that's not great for a production environment, because if we leave any dumping of data out in our templates, this is going to be visible.
00:20
So the first thing that we want to do is change this over to whether or not we have debug enabled in our application. You can even create a new config option for this if you wanted to. So we're going to say get container, and we're going to go ahead and grab out our config. So let's pull that out of here.
00:38
And we're going to say get app.debug. So now this is obviously either a true or a false value, and it will be enabled accordingly. Now, the other thing that we need to do is add an extension. So Twig allows you to extend it with multiple extensions, even custom extensions.
00:55
But we're going to go ahead and register out a extension that comes with Twig. So let's say add extension, and we're going to new up the debug extension from Twig, and now debugging is enabled. Now, to use debugging, let's work with some more complex data. So instead of passing the name down, let's go ahead.
01:14
Well, we'll keep the name. Let's pass in a bunch of users just as an example. So let's make this an array of users. So let's say ID one, and eventually we'll get to the point from pulling these out of the database.
01:28
And we'll also say name Alex, and we'll do the same thing here for a different user. So if this data was coming from the database or you weren't sure what this data was, you'll probably want to dump it out in your template. Now, if we head over to the home view down here, let's create our container, and let's just output users and see what happens. So if we give that a refresh, you can see that we get an array to string conversion.
01:54
Twig is trying to render this, but it's not a primitive like a string or an integer, so it can't convert it to a string from a pure array. Now, because we have the debug extension enabled and we've set debug to true, we can use the dump function directly inside of here to dump this out to the page. Let's go over and give this a refresh. And there we go.
02:17
So this is incredibly useful when you're just starting to play around with the data that you're passing down to your templates. And you can now see the structure of this, so you could iterate over it if you needed to. Now, let's see what happens when we go ahead and disable debugging. The one thing that we didn't do over here is only add this extension if this was enabled.
02:38
So what we could do is we could even just set a variable up here. We'll be changing this a little bit later with a helper, but we could just change this over to debug. And then we could wrap this in an if statement to only register this if debugging is enabled. There we go.
02:55
And we'll just put that directly in there. OK, that's still working. But if we head over to our EMV file and we set debug to false, when we come over here and we give this a refresh, you can see we get nothing. Now, this is broken just because we don't have that function available.
03:12
What you probably could do if you wanted this to be available is not do an if statement here. And it would still show everything because that debug function is technically available. It's just not showing anything. So actually, we could probably get rid of that and just add the extension anyway, regardless of whether debugging is enabled.
03:31
But explicitly set this here. OK, so now we know how to debug. Let's switch this back to true and we can now debug any data we are passing down. Just to finish off so we get an idea of how to start to iterate over data.
03:45
Let's go over to our home.twig file and we'll iterate over these users. OK, so how do we do this? Well, we go and use this syntax now with a percentage sign and a curly brace. And we say for then we give each of these users a variable name user in users.
04:05
So it's like a for in loop. And then here we end that for we pretty much always do this for any of the syntax in twig. Now, what we can do now that we've got access to this user is we could output the user's name and we use dot notation to iterate through. So I'm just going to wrap each of these in a div.
04:26
So they are on their own line. So let's pull that over. Let's go over. And there we go.
04:31
We can now iterate through a list of data. We'll be changing this up a little bit later when we look at database access. So instead of defining an array out in here, we'll instead grab these from the database. And then, of course, iterate through them.
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!