تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Organizing Large Views0:00

So far, we haven't really focused on our views. So let me give you a couple suggestions there. So here we have, in a fresh application, just some sample HTML that might simulate a homepage for your website. And as you know, often the homepage is kind of a unique thing. It has a lot of specific sections, each of them have their own layout, sometimes it's a bit more complicated than the rest of your templates. But yeah, nonetheless, this is going to be true in lots of cases. So I'm scrolling and scrolling. And if I want to go to a specific section, yeah, we can do things like this. You create these Blade-specific comments, and that way they won't show up in your HTML. So if I want to look at testimonials, we can go there. Here's a section that tells you to sign up. Here are featured series. Here is an about Laracast section. If we keep going up, what else do we have? This is all of your skills. Yeah,

Extracting View Partials1:23

want it to be like a top level heading to to designate that this is a new important section of your site. In those situations, in my mind, that's an indication that it should be extracted to a partial. So I might do something like this. This is my top banner or it's the header area. Well, I might include pages.home.header. Okay, so that allows me to get rid of this, I can cut that and create a resources/views/pages/home/header.blade.php file, and I can store it here. Okay, so now instantly, that's easier. So we could do this a handful more times. What else do we have skills? Get rid of that pages.home.skills and paste that in. All right, now that's redundant. Next, we have about Laracasts. And just stick with me or fast forward, you get the basic idea about Laracasts. Paste it in. What else we have featured series, pages.home.featured_series. Now, this may seem a little superfluous, but you know what,

Reusing List Partials6:18

like series.card, maybe assign a name to that. So now you would create that series.card and you would paste that in. Okay, so now, even though it's three lines, I think it's worth it. Instantly, when we come back, we can see, okay, we are iterating over the series. And for each one, we render out the card. And then if I ever need to dig in, I can go there directly. However, you can also take this a step further. Like I said, if you're always doing this each time you iterate through your collection, then once again, you can extract a partial for that and say @include('series.list'), or I often do @include('series.collection'). So now, resources/views/series/collection.blade.php. And now this is where that will live. Okay, so once again, now things are a lot easier. And this then becomes reusable. So like I said, if I have another section of my site, where I want to display related series, okay, well now

Passing Variables to Includes7:10

Okay, so once again, now things are a lot easier. And this then becomes reusable. So like I said, if I have another section of my site, where I want to display related series, okay, well now I have a dedicated partial specifically for that job. However, what if you are related series? Well, that's actually stored as a related variable. So within your controller, you saved it to related. And that is what you pass to your view. Well, that's going to be a problem because series collection expects a variable called series. Okay, well, don't forget, you can always pass variables to your partials. So when I include this, I can say, the series variable that you will expect, I actually want that to be equal to related. And now you can reuse this anywhere. You might even find in some situations, like, I don't know, SeriesCard, maybe you want to change the width of the column. So by default, it is one third or

Optional Defaults in Blade7:59

And now you can reuse this anywhere. You might even find in some situations, like, I don't know, series card, maybe you want to change the width of the column. So by default, it is one third or four up to a grid. But in some cases, you want to render out the collection and change that. Okay, so you might want to say with or class or whatever you want to call it. And in this case, you want to change it to maybe two up. Okay, well, once again, what you can do here is, well, if you're using php seven, I'll show you both ways, actually. But before php seven, yeah, you'd have to do something like this where you say, well, if you gave me a width, so if isset($width), then we're going to echo out the $width as the class the user wants to do. Otherwise, we will default to something like this. But if you're using php seven, yeah, you can get rid of that isset entirely, and just do something like this.

Otherwise, we will default to something like this. But if you're using PHP seven, yeah, you can get rid of that isSet entirely, and just do something like this. You may not have known that. And if you didn't check out the PHP seven series of Laracasts, it goes over all of this new stuff. As a quick tip, though, $name, or $joe, $name equals 'Jeff', run it again. You get the idea. So we're trying to echo out the $width if it is set. Otherwise, we will default to isThree. Anyways, you get the idea, you can expand this as much as you need to. Okay, let me show you one more thing. This is actually something we covered in one of the series, but I think it deserves a place in this particular series. So one thing you may find yourself doing is checking the type of something, whatever it might be the type of the model or the order type or the activity type or something like that. So you might say for

Polymorphic View Partials9:37

yourself doing is checking the the type of something, whatever it might be the type of the model or the the order type or the activity type or something like that. So you might say for each statuses as status, and for each, and then yeah, you might say, well, if the status type equals, I don't know if it's a video status, then I want to display it in some form. Otherwise, if the status type is equal to just a regular text, then we're going to display it differently. Else, if it's a photo, then we're going to display it differently. You get the idea. And for some situations, a good example at Laracasts, like I said, is activity, where a user could have activity that consists of creating a form thread, or replying to a thread, or upgrading their account, or getting a refund, all of those different activity types. But we'll stick with statuses. Anyways, yeah, and this can get pretty muddy pretty fast. And once again, when each of

their account, or getting a refund, all of those different activity types. But we'll stick with statuses. Anyways, yeah, and this can get pretty muddy pretty fast. And once again, when each of these has 25 different lines of code, and you're just going through this long if else or a long switch series, yeah, kind of sucks, right? So instead, you can use basic polymorphism in your views. So you could do something like this. What are we deciding here? Well, we want to render the type of status. So instead, we could include maybe statuses., and we'll do something simple, statusType. Okay, so we are dynamically figuring out what the type is, and then we are loading a partial dependent upon that. So in some situations, we are loading statuses.video.blade.php. In other situations, it will dynamically evaluate to text. In others, it will evaluate to photo. Anyways, I'll undo that. But yeah, now, this will be a lot cleaner for you.

video.blade.php. In other situations, it will dynamically evaluate to text. In others, it will evaluate to photo. Anyways, I'll undo that. But yeah, now, this will be a lot cleaner for you. Create your resources, views, statuses, and you have one for video. And here's where you do your video template. Next, you have one for text, and you do your text representation of the status, or the activity type, like I suggested. And then what else did we have? photo. And now you do your photo representation. And what's nice about this is, let's say a new status type comes into play, like you can post a status with your location, and the type of status is location. Okay, well, the cool thing about polymorphism, as it relates to your views, is that yeah, you never even need to edit this file. All you would need to do is create resources, views, statuses, location, and now you set your location template here, and it will all just work. That's the beauty of this.

to edit this file. All you would need to do is create resources, views, statuses, location, and now you set your location template here, and it will all just work. That's the beauty of this. All right, so that'll do it for this one. Yeah, you know what? People tend to ignore their views, but give them the same importance and the same respect that you would show any other class within your project.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟