Extending Blade Templates4:24 with Jonathan Barrios
In this section, we’ll use @extends to keep DRY and avoid repeating code on every page. We can extend all of the blades inside of treehouse.blade.php by using @extends('treehouse'). Blade extensions allow you to take an existing blade, or block of code, and extend it to add additional functionality to your application. In this section, you’ll be extending the treehouse.blade.php file when building views for each page of your app. Using @extends makes development much faster and also helps our code stay nice and DRY, short for Don’t Repeat Yourself.
Welcome back, in this section, we'll use @extends to keep DRY and 0:00 avoid repeating code on every page. 0:05 We can extend all the blades inside of the treehouse.blade.php by 0:08 using @extends('treehouse'). 0:13 Blade extensions allow you to take an existing blade or block of code, 0:16 and extend it to add additional functionality to your application. 0:21 In this section, you'll be extending the treehouse.blade.php 0:25 file when building views for each page of your app. 0:30 Using @extends makes development much faster, and 0:34 also helps our code stay nice and DRY. 0:38 The treehouse.blade.php file is the main page of our app, and 0:41 also serves as our master template for this project. 0:46 Next, we'll explore @extends by creating a few more pages, tracks, 0:49 community, and support. 0:54 Each using @extends('treehouse') on the first line. 1:07 Next, copy and paste library.blade.php from the project files, like this. 1:24 The @section represents the card body under the navigation of each page of our 1:36 app. The way this works is brilliant. 1:37 By using @extends, you get a head, nav, and footer out of the box. 1:44 Next, take a look at the treehouse.blade.php file, and 1:50 notice the main tags with the @yield('body') inside. 1:54 This is how we add unique content to each page, makes sense, right? 1:58 To review, @extends are the reusable components in a master template, 2:03 while @yield is the unique content that is not meant to be reusable. 2:08 Finally, let's add the @section and @endsection tags from the library 2:13 page to the tracks, community, and support pages manually, like this. 2:18 When adding the @sections manually, 2:35 you will need to change the title and description for each page. 2:38 Alternatively, you can also copy and paste the project files, like this. 2:42 This will replace the titles and the description for each page. 2:55 We're almost done building all of the views we need for this app. 3:00 But first, let's update our routes in the web.php file to reflect our changes. 3:03 Currently, our track, community, 3:09 and support pages are routed to the default welcome page. 3:11 So let's change the view names, like this. 3:15 Finally, we need to change the view name, which is currently treehouse, 3:24 inside of the AppController.php file to display the library view, like this. 3:28 Moment of truth, head over to the browser and take a look. 3:50 Nice job As you just learned, 3:56 extending blades opens up the potential for your application's front end. 4:03 You created a master template using one header, one nav, and one footer. 4:07 Then you added unique content using @yields, 4:12 keeping your files organized, separate, and quite frankly, beautiful. 4:16 Yes, PHP can be lovely with Laravel. 4:21
You need to sign up for Treehouse in order to download course files.Sign up