Using Includes to stay DRY3:26 with Jonathan Barrios
Now that we have our main treehouse view in place, it’s time to start refactoring our code to make it reusable for a more modular front-end. A Laravel @include is just like a basic PHP include, so this section will come easy if you’re already comfortable including files in PHP. First, we need to create three blade.php files so we can add the @include for the head, nav, and footer sections respectively. Let’s get started.
Welcome back. Now that we have our main Treehouse view 0:00 in place, it's time to start refactoring our code and 0:02 make it reusable for a more modular front end. 0:06 A Laravel at include is just like a basic PHP include. 0:09 So this section will come easy if you're already comfortable including 0:14 files in PHP. 0:18 First, we need to create three blade.php files, so 0:19 we can add the at include to the head, nav, and footer sections respectively. 0:24 Let's get started. 0:31 When we're done adding at each include, 0:33 the treehouse.blade.php file will shrink from 60 plus lines 0:36 of code to just 11 lines of code and looks something like this. 0:41 Pretty nifty? 0:48 To get started, create a new file named head.blade.php and 0:50 add the whole head section from the treehouse.blade.php file. 0:56 Remember to delete the entire head section from the treehouse.blade.php file and 1:08 add the head @include in its place 1:14 Next we'll want to apply the same treatment to the nav tag. 1:22 So let's create another new file named nav.blade.php, 1:27 And add the whole nav section from the treehouse.blade.php file. 1:36 Don't forget to add the nav @include in its place. 1:59 We're going to skip the main section for now and 2:13 add the last @include by creating footer.blade.php. 2:16 And adding the footer @include from the treehouse.blade.php file. 2:39 Don't forget to delete the footer section from the treehouse.blade.php file, 2:43 and your file should look something like this. 2:49 Nice job, you removed the duplicate head, nav, 2:57 and footer tags by creating a massive template that's modular. 3:00 Nav bars, tables, and 3:04 many other standard HTML components can be handled this way using includes. 3:06 Next, you'll build all of the views, one for each one of our pages. 3:12 After creating views, we'll display courses from the course models on 3:17 the library page, just like Treehouse. 3:22 See you there. 3:24
You need to sign up for Treehouse in order to download course files.Sign up