6.2 Using a Presenter
Sometimes we need just a little bit of logic in our views to display information, but the view is no place for such logic. Instead, we’ll use the Presenter pattern.
1.Introduction1 lesson, 01:23
2.Getting Started4 lessons, 46:41
3.Managing Pages6 lessons, 1:12:31
4.User Management2 lessons, 27:37
5.Managing the Blog4 lessons, 41:51
6.Adding Extras2 lessons, 26:07
7.Implementing the Front-End3 lessons, 30:24
8.Homework Review1 lesson, 07:11
9.Conclusion1 lesson, 01:24
6.2 Using a Presenter
There are always the times when we need to perform just a little bit of logic on some data before we display that data in the view. And that always raises the question as to where do we do that logic? Do we do it in the view? Well, no, we don't. Do we do it in the model? And once again, no, we don't this is something that is specifically for the view. It's for displaying the data and nothing else. So it doesn't make sense to put it in the model as well. Like for example, here we have our list of pages. And while it's okay that we just have a list of pages, it would be nice to have some visual cue as to see what page is a child of what other page. So, the fact sheet is a child of about, so it would be nice if there were some indentation here. And that's logic that doesn't belong in the view. And it's also logic that doesn't belong in the model. So this is where something called a view presenter, would be very useful. And there are many view presenter packages that we could use. Unfortunately, it's not built into Laravel, but there are many packages and the one that we are going to use has been written by Jeffrey Way. It is simply called Laracasts presenter, so we're going to say composer require, layer casts presenter. And while that is installing, we are going to go to our project and inside of app, I'm going to create a new folder called presenters. Now, it doesn't matter where you put your presenters. I'm organizing this so that all of my presenters are going to be inside of this folder called presenters and we're going to create a new file called page presenter. So we typically have a class for an individual model. So page presenter, let's have our PHP tag. Let's also have the namespace of app presenters. And we also need to use Lararcasts Presenter Presenter, because all of our presenter classes are going to inherit this presenter class. So class page presenter extends presenter and then the methods inside of this class are going to be used inside of our view for performing whatever logic that it needs. So we need a function or a method rather for padding the title. So let's just call it padded title. And the first thing we need to do is get the padding, which we will just set to as an empty string right now. And then we will return padding, we will concatenate this and title. But before we can even use this, we do need to go to our model and we do need to add a trait here. So we first want to use Laracasts PresenterPresenter or no presentable trait, that's what we want, and then we will simply use that trait inside of our class. But then we also have to specify what presenter, this model is going to use. So we do that with a protected field called presenter. And then it's simply the path, so App Presenters PagePresenter. So there we have set that up so we can close our model. And let's go to our views, so that is resources, views, admin, pages, index. And instead of using our title property here, we are going to say present, conchoid that method. And then we're going to use our padded title, but we aren't going to call it as a method, we're going to use it as a property. So that's going to give us whatever that is going to give us, right now it's just going to give us the exact same thing. Because an empty string is, well, it's empty. So let's do this, let's just add some dashes so that we can see what that looks like. Now right now it's going to add dashes to everything, because we haven't taken the depth of the page into account, by depth I mean how deep in the tree it is. So you can think about the roots of something, about is one of those roots pages so it's depth will be zero. The fact sheet is a child, a direct child of about so it's depth would be one. If there was a child of the fact sheet, then that child's depth would be two. So basically what we want to do is take the depth of the page. And use that to calculate how many spaces that we are going to add to the title. So we could do something like this, we can say, string repeat and we're going to use the non breaking space. So we need an ampersand, non breaking space. And then we have a property called depth. Now, right now we aren't going to get anything for this value, we're going to have to make some modifications to our controller, but this is going to be fine for now. We're going to take our depth, and then we're going to multiply it by four. So there's going to be four spaces, for each level of child, so let's refresh the page. We just don't see anything except the dashes go away. So here's the thing, the depth isn't given to us by default, we have to explicitly say that we want the depth as well. So we wanted to go to our pages controller. And this is for the index, so we're going to say default order, I mean nothing's going to change there. But then we want to say, with depth, that is going to include the depth so that we can then use the depth property. So now whenever we view this, we're going to see, well call to undefined method Query Builder, with depth, it helps if you type it correctly. So we will say with depth, and after we refresh the page, we see that our presenter is one. Working because we see our spaces. The problem is that we literally see the entities and that is because of the default behavior for blade expressions. Everything is automatically passed through the HTML special chars function by default. And we want that, I mean that is a good practice. But in this particular case, we know that our Peda title is very safe. So we are going to opt into the syntax of outputting raw HTML. And so now if we go back and refresh the page, then we will see what we would expect, we see about and then we see a little indentation for the fact sheet. And then of course we see contact and if we wanted to edit contact, make this a child of the fact sheet. Then we will see a nice little tree hierarchy, where about is at the top followed by fak, and then contact. But let's change the contacts to be after about, just so that we have one thing that is a child about. Now, it would be also be nice to have the same formatting for our drop down, so that we can see the relationship between these pages. And this is easy enough to do, we have everything available. We just need to make a couple of changes. The first, is we need to call with depth inside of the Create Method. After we call default order, we want to say with depth and we also want to do that in the Edit Method. So after default order, we will call with depth and then get, but we also need to modify our view. So let's go to our fields.blade.php and we'll, we output the title, we still want to opt in to outputting raw HTML. We also need to change this so that we call the present method. And then we use the padded title property. But with that done, we will then see our list of pages nicely organized, so that we can see the page hierarchy.