Locked lesson.
About this lesson
How to use Require to template things.
Exercise files
Download this lesson’s related exercise files.
Templating With Require.docx59.3 KB Templating With Require - Solution.docx
59.3 KB
Quick reference
Templating with Require
In this video we'll look at using require for templating.
When to use
When your template is necessary to the app, use require.
Instructions
To recap, using require works like this:
require 'filename.php';
You would use it if the data in the file is necessary to your app. Remember, if there's a problem, require will crash your app.
Hints & tips
- Use require when the data in the required file is necessary to your app (your app can't run without it).
- 00:04 In the last video, we templated out our header, all of our links, and
- 00:08 all this stuff at the top of our page.
- 00:10 In this video,
- 00:11 I wanna talk about using variables without declaring them on every page.
- 00:16 Now we've sort of talked about this with session variables that's not what I
- 00:19 wanna talk about.
- 00:19 We've also sort of talked about this with down here these form passing variables and
- 00:24 these hidden fields.
- 00:25 That's also not what I wanna talk about.
- 00:27 Instead, I wanna talk about creating our php on a different file and
- 00:32 sucking it in to a current file, and kinda weird to talk about it in the abstract so
- 00:36 let's just actually do this thing.
- 00:37 Come up here, right click and create a new file.
- 00:39 Let's call it random.php.
- 00:43 Okay so I'm gonna come over here and I'm gonna grab this bit of code.
- 00:48 Actually I'm gonna grab all of this code right here.
- 00:50 And I'm gonna copy it, and I'm just gonna paste it into our random file here.
- 00:55 And let's see, don't need this.
- 01:01 Okay.
- 01:02 So if we save this, all this is doing is this file is just creating these two
- 01:06 random numbers that's naming them num1 and num2.
- 01:09 So if we come back to our index page, our addition page, up here at the top,
- 01:14 let's create another php tag.
- 01:16 Or we could've done it, actually, let's just do it like this.
- 01:23 Make it all one tag.
- 01:25 And let's go require and random.php.
- 01:30 Now if we save this and come up here and hit reload,
- 01:34 go to our addition page, nothing really has changed, right?
- 01:38 Well, since this random page doesn't have anything that it's outputting,
- 01:42 it's just creating variables.
- 01:43 It's not outputting anything.
- 01:45 If it was echoing these out, let's just echo them out and see what happens.
- 01:51 Save this, come back here now and hit reload.
- 01:53 We have these numbers up at the top of the screen, 3 and 10.
- 01:56 Well, that's not what we want.
- 01:58 So we don't wanna do that.
- 02:02 But these are still now available to us, so if we come back here to our index page,
- 02:07 we can get rid of this code right here.
- 02:11 So we're no longer generating random code on our index page.
- 02:15 What's happening is when index gets called, this require tag gets executed.
- 02:21 It opens up and runs this random.php file, and it generates our random numbers.
- 02:27 And now we can use these two variables Inside of this page however we want.
- 02:32 So we save all these stuff, come back here and hit reload.
- 02:35 7 + 2 = 9 if we submit that.
- 02:38 Yes 7 + 2 = 9, 7 + 3 = 22, wrong.
- 02:42 7 + 3 So our program continues to run 4 + 2 = 6,
- 02:47 8 + 6 = 324,234, 6 + 6 = 12.
- 02:51 So like I said, our program continues to run,but there's just a little
- 02:55 bit less code on our index page.
- 02:57 Now if we went through and did that for all of these pages there will be a little
- 03:01 bit less code on all of those pages and this is just one quick little example how
- 03:05 to abstract off something from your page onto a separate file.
- 03:08 And it's always sort of a good idea to keep your code on
- 03:12 one file in your HTML on another.
- 03:14 Because if you look at this HTML file.
- 03:17 Actually it's php file, but it's,
- 03:18 this is our HTML page, there's a lot of code going on here.
- 03:22 And it's not always a good idea to write your code right on a web page itself, for
- 03:26 just a lot of different reasons.
- 03:27 And using the require like this allows you to, like I said,
- 03:31 abstract that code to a separate file and also,
- 03:33 we could change this in the future if we want it on this just one file only, and
- 03:38 then it would, the change will appear on all of these different files if we went
- 03:43 through and create that require on each of these pages.
- 03:46 So just something I want you to sort of wrap your brain around, not terribly
- 03:49 important for this app, because it's such a small app, but it's an important part of
- 03:54 templating, keeping your code separate from your HTML and that's how you do it.
- 03:58 So two examples, one with include, one with require.
- 04:01 I tend to use include for things like visual stuff.
- 04:05 Links and headers stuff.
- 04:07 I tend to use require for code things that are necessary.
- 04:11 If our app doesn't generate these random numbers, it's good that it crashes, right?
- 04:16 And require, if require doesn't work right, it will crash your app, so
- 04:20 that's kind of a good thing in this case 'cause that stuff is necessary.
- 04:23 So that's all for this video.
- 04:25 In the next video we will finish things up.
Lesson notes are only available for subscribers.