PHP Projects: How to Create a Database-Free, Markdown-Based Blogging Application | John Morris | Skillshare

PHP Projects: How to Create a Database-Free, Markdown-Based Blogging Application

John Morris, I help freelancers get clients.

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
15 Lessons (4h 13m)
    • 1. Introduction and Overview

      3:39
    • 2. Rewrite URLs With .htaccess

      6:59
    • 3. Parse URLs and Determining Content Types

      16:36
    • 4. Creating and Loading Post Categories

      10:10
    • 5. Determining and Setting the Template Type

      16:49
    • 6. Parsing Markdown and Building the Content Object

      32:41
    • 7. Create the header.php Template Part

      34:59
    • 8. Creating footer.php and Blog Widgets

      29:06
    • 9. Build the sidebar.php Template Part

      2:35
    • 10. Build the Page Template

      25:17
    • 11. Build the Post Template

      20:35
    • 12. Build the Category Template

      26:23
    • 13. Build the Homepage Template

      5:12
    • 14. Create a Custom RSS Feed

      21:43
    • 15. Questions

      0:41

About This Class

In this PHP project, I'll show you how to create a markdown-based blogging application with no database. The main advantage of an application like this will performance. Without the overhead of an admin interface and database queries, this application moves very fast. 

But, beyond that, as a class it's a great way to deepen your PHP skills. 

You'll learn how to do things like:

  • How to Rewrite "Pretty" URLs
  • How to Create a Template Handler
  • Mapping Content to Templates
  • How to Write PHP-Based Blog Widgets
  • How to Create a Custom RSS Feed
  • Website Templating

And more.

This is my most advanced website template to date. If you really want to dig into some advanced PHP and learn how to create PHP applications, this course is for you. Source code included.

Transcripts

1. Introduction and Overview: All right. So let me show you real quick what we're going to be building inside of this course. So what this is is a data baseless markdown based blogger application. So there's no database involved with this. The way that you format text is using markdown, which is sort popular in certain circles. And then it's sort of a what you would expect from a blogging application. So on the front page, we have a list of posts we can click through and read individual posts. We have categories that Post can go into Ah, we have a sidebar with. In this case, we have a list of categories in the post. We have some footer widgets down here. We have admin. Page is like an about page. Ah, and then we also have an RSS feed. We click. We can see we have our RSS feed here, so it's sort of a again sort of a full functioning ah, blogging application. And just to give you a real quick example of how to add content, I can come over here into my content folder again. No database. No, you I Ah, this is sort of just a full back end thing. So I'll open this file and will copy this code and let me just create a new file, and we'll just call it Hot Dash Dog, all right? And we'll open that up and more paste in what we copied. And we just changed the information in here. So let's call this hot dog. The name of our file or slug is Hot Dash Dog. I wrote. It will change the date to the 30th. Um, and let's just let's sort of get rid of this bottom part of text here so we can see the difference. All right, so if we save that and now we come back over and we go to our home page. Now we have a new post called Hot Dog. May 30th by me, we click through, we have this post. It has its own. You, Earl. Ah, it shows up in our latest posts. Over here, over here, etcetera. I didn't I didn't put it in a category. But if we put in categories sure up there as well, etcetera. So again, it's a It's a really simple sort of blogging application that's all running the back end. Know you I know database and one of the advantages. One of the reasons why this might actually be something you want to use for yourself or clients or whatever is just performance, because I'm on a development server, so it's not necessarily the most powerful server you could have. But I can click around into things in here, and it moves extremely fast. And that's because there's no database reading and writing or any of that stuff. So it's and there's not a ton of overhead in terms of the code, so it's very, very fast performance wise. So that's what I'm going to show you how to build. I'm gonna show you how to build the entire back and framework and then show you how to build the front and template, how you can change the templates and create different templates if you want. So you gonna learn Aton about PHP. You're gonna learn a ton about template ing concepts you are. L rewriting um, and an HTML and CSS, of course, along the way, and I think just give you sort of a a better understanding of building applications in general. That's what I ultimately want you to get from this. It's not so much about this individual application as it is sort of stretching your mind and thinking in different ways about the ways that you can build applications and then learning different techniques for doing that along the way. That's what I want to give you. Ah, inside of this course. So that's what we're gonna dio. So that said, Let's get into it. 2. Rewrite URLs With .htaccess: All right, So the first thing I want to cover is U R L rewriting and handling. So what we'll do is we'll create a folder here for our project. I'm calling this Rourke, and we'll credit file in here, and this is going to be an HT access file, so dot H t access and what we need to do in the HT access is we need to rewrite and sort of redirect our girl. So will do if module mod underscore rewrite dot c. So we're gonna This is basically we're only going to do this if Modry right is actually installed and enabled on the server. Otherwise, we can't really use it. So ah will wrap this in that if block and then what we want to do inside here. So the way that our our applications me structured is if you've ever worked with something like WordPress, I'm sure the juniors in the Drew pools of the world do this, but you'll go to a page, for example. That's no your domain dot com slash about. Now, if you were to go on the server, that about page wouldn't actually exist at your domain dot com slash about, Okay? And so you again if you've worked with WordPress, you know, this and so forth. So the way that that works is we You is We use moderately right here, and we use these rewrite conditions too. Any request that doesn't request something that doesn't exist on the server were gonna pass it to our handler, and we're gonna try and handle it. And then if we still can't find anything, then we are going to throw 404 So to get all of that started again, we need to to start with, making sure that we're able to grab all of those different files for something that is not doesn't exist. So first thing we're going to do here is turn the rewrite. Ah, engine on. So the right engine on and then we're going to create a condition. So this is like essentially an if statement. So if we do rewrite and we're going to do Conso rewrite, rewrite condition and we will do our percentage sign, and then we're gonna do these curly brackets and we're going to do script, file, name and Okay, so an exclamation point, Dashti. So essentially what? This is saying is if the requested file name doesn't is Ah is it's a directory that the server think is thinks is being eyes being requested doesn't exist. That's one of the case condition. So if and then the other one is essentially the same thing, except instead of a directory we're going to do, we're gonna do a file. So dash F So this is going to any files or directories that someone someone tries to go to that don't exist on the server they're going to be passed to. They're gonna meet these conditions and then they were there. Those requests are gonna be redirected to where we set our rule here. So we're going to rewrite rule, and this is just at the beginning here. This is essentially some wild card stuff. Ah, what this is saying is, essentially, we want to past all of the entire path that was requested. We're going to send that to in deck, stop PHP, and we're going to store it into a query variable. And that query variable here or or you are our parameter here is going to be Q. It can be anything you want, but that's just we're doing Q for queries were keeping a simple and then el que s a Okay, So if any of any file that is requested doesn't your directory eyes request that doesn't exist on the server were gonna send it to index dot PHP and we are going to put the requested path into a query parameter Ah, named Q. So that's that's what we need for our, uh, HD access file. Now real quick. Let's go ahead and let's create a new file called Index Stop PHP and an index dot PHP. This is actually where our entire this is where we're gonna load up everything eventually. But for now, what we can do is let's just do PHP and let's do print are and request Okay? And let's come over here. Teoh our page here. Now, if we were on the home here, then we noticed we get a blank array. But if I say something like about then you see, I get q equals about. If I do category slash dog slash cat slash monkey slash apple, I get q equals category slash dog. So whatever is requested, it's going to be in this queue variable so That's the start of all of this because we need to be able to know what was requested. And then from there it's just a matter of now that we have that path that was requested. Well, now we can handle that in our euro. We can check eventually the content that we create to see if we have a match. Ah, and so forth. But to get everything started, you need to set up this HT access file how we've done in here and start to you Are you are l rewriting? Now I'll drag this down here just real quick. You'll notice Ah, it doesn't. We don't have index stop PHP in here anywhere. And you'll also notice that even though we have this Q parameter and it if we let me do this real quick, if we do get like this and we refresh that, you'll notice that we still have Q equals. But there's no you are pro l parameter up here. That's what the rewriting does. So this is how we get thes thes pretty links that that you may have tried to do before. See, people do before. So ah, the even Though the query variable is not actually gonna be in the URL. It is still there in the request information, and that's what the each to access file does for us, all right? 3. Parse URLs and Determining Content Types: right now that we got ht excess file set up now, we can start getting into some of the handling of this. So I'm going to show you the structure that I sort of created for this. Obviously, you can kind of do this your own way. Um, but this is just sort of what I I like to do. What I what? I did. So in our work folder, we're gonna create a new folder called Live. And This is where we're gonna keep a lot of our sort of application centric type files. And then inside of this will create a new folder called Class. And this is we're going to store all the classes that that we create and use. And then inside of this will create a file called Rourke. And that'll be work dot PHP Work's going to be our main class for our template handling and so forth. And so we need to make sure that we have that over Ah, in our ah index dot PHP file. So what I'm gonna do, I'm gonna do a couple things. One I'm gonna drop in this code right here. Essentially what this does. Is it just makes us so as word developing this, we can see all of the errors. Of course, for production you would get rid of this, and then we're gonna do a require statement. So we're gonna do require once, and we're going to use Ah dura. It will get. This is just like doing Duerr name and then file. So it's going to give us the path to the current directory and then dot and then slash lib slash class slash Rourke dot PHP. So what, we just ah, the file we just created We're going to include that. And then we're going to do Rourke equals new Rourke, and we're going to haven't created this class yet, but we're about to. So we're loading in our work file, and then we're incensing new instance of that particular file, and then that is where we are going to If we open up work dot PHP, then this is where we are going to do the bulk of are handling and so forth. So will create a new class old work, and the first thing that we need, we're gonna have a Siris of properties for this. But the 1st 1 that we need is going to be slug. Now, you know, there's probably some debate that we could have about whether it should be private or protected, etcetera, etcetera. I, I usually just start off, is protected unless I need some other. Unless something comes up in my coating that I need, Ah, I need to make it private or I need to make it public. That pushes me 11 way or the other. That way they're going to be available inside of this class and any child classes or any classes that extend it. And that's sort of to me seems like for me is the default position. So that's what we have here. And then we're going to create a constructor, and we're actually going to do some things in our constructor. And matter of fact, most of the work is going to be done inside of the constructor. So the first thing that we need to do is if you remember, we have this path here now that was requested. Well, now we need to start analyzing that and breaking it down and figuring out what was being requested. And so the term I'm using is slug. And so what we want to do is we want to get the slug from from that Ah, from that query parameter. So we create a method called, uh, we're gonna set this slug equal to something. Ah, function we're going to create in their method we're going to create in this class and we'll call it slug from Or we could just really just call this get slug case. We're going to say, get slug is going to be the name of our function here, and then we need to actually write this function. Ah, slug again. I'm defaulting to protect it. Unless I end up having some other reason not to. And we're doing get slug and then and get slug. What we're going to do is we're going to use that query parameters. So we're going to deuce. Queary equals and where I'm gonna use filter input. The reason I use filter input and we're gives input get, and we're gonna do que The reason I do filter input is because if the query parameter doesn't exist, it doesn't throw an error. It just returns knoll, so I don't have to do in it. And if steam in. And it's sad and all that. I could just do filter input. It sort of takes care of all of that for me. So we're gonna get that query parameter. And if we just at this point, if we just echoed query like this, uh, we've done everything right up to this point. We should just get on. Let's see. So something's wrong with our Well, we forgot to put function function, do that a lot. So here. And we've got down here. But function okay? And so here we have What are query parameter was now just echoed out. Okay, so that's a start. So we've got it now captured into this query variable here and then from here, we we we just need to sort of break it down and figure out what it is. Okay, So the first thing is, we want to check and see if it's empty, because if we go so here we're on this path right here. But if I go to just Rourke like this to the root, you'll see it's empty. There's nothing there. Okay, So that needs to be handled because that is our home page. So we should be displaying something there. That's not ah ah, 404 or anything like that. So what we're gonna do is I'm I like to be sort of I'm gonna use the word verbose a lot, which essentially means sort of really drawing things out in the code. So I'm gonna really sort of be specific about all there something to do if false equals. Ah, query or no equals Queary. So we're basically checking for false or knoll and filter input returns knoll if it doesn't exist and false if it tries to run the filter and the filter fails. Now, in this case, I didn't pass in a filter. All right, this is this third parameters where you put in some sort of filter like filter default is what is ah, what it does by default if you don't pass anything in. And what filter default does is nothing. It essentially it it uses filter unsafe Raul, which doesn't filter the parameter at all, which is what I want. I don't want I don't need it. Ah, for what we're doing here, I don't need it filtered. I just want to make sure it exists so but if for some reason, it tries toe use filter unsafe Roland filter and returns false when something's going on there, because it shouldn't do that. So I'm just checking for both conditions here. And if either one of those is met, I'm actually gonna return. Not false, but home, because now we know we're on our home page. Okay, so we've got the first bit of handling, and we could do something like this Eco home. And now I'm on the home page here and we see we get home. If I go to say, slash about Well, then I don't get it because I'm not on the home page anymore. Okay, so that's again our first sort of bit of handling, right? But if that's not, if it's not basically empty, then we still need to sort of handle it. And so knowing where I sort of know where I'm going with this, there are certain euro. The thing about this is there's gonna be certain u l r l structures you support and want you don't let's play if you look at application like WordPress, when you go into the Perma link settings, you have to tell it what structure you want it to use Because you have to tell it what structure you structures you want to support. And so in here were just sort of doing that in the code. Okay, you could, of course, abstract that out and so forth. But in our case, we're just going to do it in the coat, since we don't have a database and all that sort of thing. So the one the sort of one catch to all of this is you're going to have post post pages that will have just a single slug like the name of the page is cat. It would be Rourke slash cat, and if it's ah, or you'll have a page like slash about. But you might also have categories that would be slash category slash dog. So you need to handle both of those situations. So what we can do is I'm just gonna do parts equal to we're gonna explode. What we get from the string that we got so well, actually turn this back to return and we're gonna explode it, and we're gonna explode it on slashes because to be ableto essentially deal with categories . So if we do print are and parts and on the home page we're not going to get anything because that we already handled that situation. But if we do, say slash dog, which might be the name of poster, let's do slash about, you see that we just get an array with about in it. However, if I do category slash dog, I get an array that has category and dog in it. So we're breaking apart to your l and we're getting a structure that we can start to work with. Okay, so now we have an array that has the parts of the URL broken down by slash. So there's a couple things here. We're only supporting categories, and that's only going to break the query that we grabbed the path into a max of two parts. Right? You're only gonna have slept category slash dog. You wouldn't have categories slash dog slash cat, right? That wouldn't make any sense. So and we went We don't need to support that for any reason. So what we're gonna do is we're gonna get the count of how many items are in are a race. We're going to use the count function in PHP, and we're gonna pass in parts that will tell us how many elements air in that, If you count is, uh, count greater than two. Ah, we know that that's an invalid, your EL or one unsupported euro. Because in our category, the most we will ever support here is to and everything else are post pages are our admin pages and so forth. Those are all just gonna be one element. So they're not going to be greater than two. So anything greater than two is an un supported U R L. And what we can do is recon turn 404 for the slug. Okay, so right now again, all we're doing is figuring out what the slug it. So we've got home. We've now got a 404 here. Ah, so if it's greater than to turn a four or four, Otherwise, if account is equal to two Well, now we know we've got a category, and so we can do a couple things. Now we could assume that this was category. However, you know, maybe down the line, you want to do tag like this. This might give me America's so again that assuming that that is ah category isn't necessarily the right thing to do here. So what we'll do is we'll just check it. We'll see if parts zero is equal to category. Now we know if this is true that we have a category here. So if parts zero so the hello the the element in the first part right here, which in this case now is tag. But it will be category in in reality. So if it is that, then we're going to return and I'm gonna use Sprint f. And the format is going to be and category dash and then are place holder like this and then we're going to put in Parts one. Okay, so so what? This is going to do if let's just that's just echo slug up here, since we'll be a little bit easier. So when we're on a category page or actually refer on tag page like this, it's not going to It's not going to do that. There's a scene, no slug. It's set because nothing's matching at this point. If we go to category slash dog and let's see this stuff this slug we go to category. Then we get category dash dog for your back to tag. Then that should give us 404 Okay, so this is you can see we're starting toe. Get somewhere figure out what the's are now I'm doing category slash dog so that later on I know that I'm dealing with a category and that will become important a little bit later. The last thing here, then the last condition here is going to be essentially everything else and everything else . Ah, we're just going to get the last item here and I'll show you essentially because we've done anything greater than two is a 404 That means to to infinity is handled. Then we did. It's equal to two that that means to is handled. So too and greater than two is handled Well, what's below that is one, and that's what this is. So if there's Onley one element that means we're not on a category were on something like our about page like this. And in that case, we're just gonna get that slug. So we're using a ray pop because there's only one element in that array, because again, we're literally checking for that here is just gonna grab that one element and return that as the slug. And then, of course, I you always put in I'm in a return false. If for some reason we get through all that and none of this catches would just return false , because something's not right. Okay, so we've got we've started to now sort of get into breaking down and handling. Are you Earl and analyzing it, we figured out if it's the home page, if we should, uh or if it's a 404 page, if it's a category page or if it's a pay poster ah, Post or a page, we're gonna have both posts and pages, which are kind of like admin pages, like in about in a Contact and so forth came. So that's what gets slugged. Does it takes that query? Your Ellen start to do some initial analyzing from that, and then, from there, we can start to get into getting more specific about figuring out what content we need, what templates we won't load and all that sort of thing 4. Creating and Loading Post Categories: all right. So the next thing we need to do is we need to start to set up our content structure a little bit, and specifically, we need Teoh get we need to set up our categories because we're going to need that in order to be able to do some of this next stuff that we want to do for or class here. So what we're going to do for this is under work will create a new folder and we'll call this content. And again, this is just the structure that I happen to choose. You can course, structure this however you want, and you'll see that as we get into this that you could reference these files wherever they happen to be that that part of it's not really that big of a deal. But we'll start off and we're gonna have a folder, a new folder called Page Under Content. So that's where we put all our pages, will have a new folder, Cold Post. That's where we'll put all our posts and then we're gonna have a new file called categories and categories. There gonna be a little difference. It's they're gonna be in there it's your going to specify them all in this one file, we just have ah, file and then for post because you would have multiple posts. Ah, that would need sort of their own. Their own file. You have a folder for those, but under categories, then we're going to We need to sort of just create some some categories here. Now, I'm gonna This system uses Jason, so I'm gonna just drop this in, um and show this to you. This is just sort of standard, Jason. It's a Jason object. We have two categories. One named. Ah, demo. You can see the information about it named slug The Post that are in it. Ah, and then the content, which in this case is a category description and the markdown will be supporting that another one called test here. But this is just sort of a standard Jason object here. If you wanted to add Ah, a new category here, you would simply add a comma here, and then you just copy and paste what was above. Then you just come in here and change all of this stuff here, okay? The big thing here is this list. Suppose Now, the reason we do it this way is because you could specify the category in the Post. But when you visit a category page, toe list all of the post for that category, you'd actually have to go through every single post and check to see if it belonged in that particular category. That would be very start, especially had a lot of posts that would not work out well, performance wise. So instead, we create a file over here, and it's sort of like you create a post. And then in a lot of times in software, you'll go and you'll check ah, box or something like that to put it in the category here. You just come over to the categories file and you put it, you add it to this Ah, this element right here. In order to put it in that particular category again, we're doing everything without a database. So some stuff is a little bit more cumbersome, but just sort of the way it goes. I'm gonna go ahead and get rid of this one. Don't really need it for right now. And so now we've got our categories over here, but this is a Jason object. So we need to, you know, we need to be able to deal with this a little bit. Ah, we need to put it into our PHP can actually read it and so forth. And so we're gonna go back over to lib, and we are going to create the first of our functions files. So we're gonna create one called category dot PHP and you see, as we go through this, these are not gonna be classes. These air going to be just straight up. Ah, functions and functions files. But we're gonna ultimately end up with you. No one called Post one called rss one called Image for handling those different parts of the templates. Now, we don't haven't talked about templates yet, but essentially, what we're putting in these function files are template tags that you can then use inside of the templates that we're going to create to do different things like display the title or an image or the categories or so forth. But we can also use it inside of our Rourke here as well. So we're gonna go ahead, open this category dot PHP and we'll talk about that Ah, in a second. But before we do that, we need to make sure that we go back over to index stop PHP. And now we have a new file here. And so we're goes going to do a choir once, and we're gonna go live and then category dot PHP Now you could do auto load and all that stuff. If you know all that, that's fine. But I really like to be able to see explicitly what was what's loaded in what order And also just sort of for doing courses in tutorials. Uh, doing the auto load could be really, really confusing if if you haven't learned that yet and I don't want that to be something bogs us down. So I'm just loading everything, sort of explicitly here. But we just created that file. Now we've got it loaded here into index dot PHP And so now we can sort of use it, and we can actually use the inside of our Rourke class, which we're going to do. Ah, so with that said, then we can come over here to category, could actually write this function. And so when this is going to be a function called get categories. And now we also with this we need to come and we need to create our conflict file. So because we need to specify are absolute path. So we're gonna do con fig dot PHP. We need a constant here. And let me just open this up pro quick. All right, so we're gonna go to conflict dot PHP and inside here, of course, open our PHP. These are gonna be apt. Constance will also have one that's gonna be app options, which is a little bit different, but we're going to do define, and we're going to do a constant called abs path, and we're gonna do during name five. Actually, we could probably just dio I haven't actually tried this yet, but of this, dirt is a little bit one that's a little bit newer, but we're just going to do dura like this. Okay, so what that's going to do is is going to give us the path to the directory that con figures in the absolute path to directly the conflict is in Well, that's our slash dot Rourke. So that's our absolute path. That's what this constant is. So this is sort of a a starting point that we can use as we for when we need passing throughout the rest of the application instead of having toe constantly. Ah, figure it out. We can just use ABS path as a constant now, and we will have the root here in work. Okay, so again, because we've just This is sort of a new thing that we just added. We need to come back over to our index, stop PHP, and we're going to require again. And, uh, dir dot con fig dot PHP. Okay, so that's going to get us our conflict file in here and now with that said, we can come over to category and write this function. So the foot we first we need the category file that we created here. So this categories file we created in content here. So we're gonna do file equals, and we're going to do file, get contents and the file that we want to get or the path we want to get his abs path. And that's gonna be dot content and categories. Okay, so that's again, ABS path gives us Rourke, and then we get content and then categories that's gonna That's gonna be the path to the file and then for categories we're just going to do on. Actually, I'll just do it like this Return J. Sohn decode file True. Like this. Okay, so what that's going to do is is going to grab this categories file here, And this is Jason. It's going to decode that, Jason, and it's gonna return that, and we can come over here to Rourke. And now we can create a ah, we can create our constant We need up here called this Categories. And we're gonna need this for in several places throughout this, so set that equal to get categories and then let's just do print or on this categories. All right, so everything worked out, which probably didn't but and actually did. So we get we get an array, a PHP array of our categories. So demo name, demo slug, and then tests and then test slug etcetera. So we have our categories now, over here, stored as this this categories, property, and we can use those and we're gonna need those throughout several places, including the next thing that we're gonna cover, which is when we get into setting up our template 5. Determining and Setting the Template Type: right. So the next thing we want to do is we're gonna figure out our exact template type. So the first thing we're gonna come up here and we're gonna sit protected and we're gonna dio we have already done slugs were going to template type, and actually, we created categories. So we're going to do categories here. We'll clean that up a little bit from last time. So we have three different properties right now slug template, type and categories. And we're going Teoh, get rid of that. And so we're just gonna set. Ah, we're going to call it or use a function. We're gonna call it in our construct, remember? Right now we're all we're still just in our constructors. So this is all happening when we insensitive new instances class. So we're gonna do set template, and then incentive and set template we are going to figure out and set or template type. This will come down here and this is this method is going to spawn several others and you'll see why here in just a second. But we'll do function, set, template, and essentially, this is gonna be a big switch block and but we need a few other functions. Ah, for a few reasons in order to make this work. So it's gonna be a little bit of a unique switch. You may or may not have seen this before, but we're gonna do switch, and then we're going to do true. And the reason why we're going to do that is because in our case is inside of our switch, we're actually going to be running comparisons. And if they evaluate to true, the 1st 1 that evaluates to true, that's the one that will be used and the switch will break at that point and we'll move on , and that's what we want. So the first match, essentially that we get here is going to is going to be the template type that gets set. So we start off with the first case is, is this slug? So remember we just got figured out. What slug is this slug and all that we did just did that method. So if that is equal to home, then we're going to set this template type equal to home and then we'll break. Okay, so, essentially, the way this works, because we're switching on true. And then our cases an evaluation here. If this evaluates to true, that'll match up with this and therefore this template type will be set and will break. And that'll break us out of our switch block. Okay? And that's so that template type is what will get set. That's the way this works. Um, and we do that because we have to do this evaluation. We didn't have to do it with this one, but we will have to do it with some others. Okay, so we're checked for home. We're also going to do Ah, we're gonna check for if it's where I'm sort of getting the easy ones out of the way first . So we're going to do if it's equal to feed. That means we know we're on our RSS feed. And so we're gonna set, uh, we're gonna set template type equal to RSS, and we'll break out of that. Ok, now, here's where it gets a little more tricky because if you remember, our slug was It was home. It was 404 It was a category dash something. Or it was just whatever the name of the poster the page waas Well, now we need to figure out if it's a post or a page. We have toe have to start figuring that that part of it out. And so we need to spawn a several their methods in order to do that. So what I'm gonna do here is I'm gonna sort of speed this up a little bit, and I'm gonna drop in some code. Ah, that I've got here and we'll go through it. Um, but essentially, it's the rest of this switch statement. So now that you've got the idea for it, what we're what we're doing here now is where we were going to create a method called Is Page one called his Post. And one called is Category and that's going to check whether the slug is a page, a post or a category. So it's actually gonna check to see if that call intent exists. Okay, so we've already got our categories. Ah, set up. So we want to populate some content into our posts on our pages, so I'll drop in some dummy content that I've got here for this and let me grab the about page really quick and actually will drop in all these So far Page we have are 44 our home and our about. But so if we open up this page here we have What we have in here is we have a Jason object which has all the metadata for this particular post title slug author, date, all that sort of thing. And then we have three equal signs, and then we have some code here, so we're gonna be able to come in and be able to split this document by these three equal signs, be ableto parts out our metadata and then parse out our content. And then we're going to use our markdown parcel, which we'll talk about later, toe, actually parse this. So again, this is a markdown powered application. So we're going to use a markdown parse er to be able to mark this. So these dashes right here will be turned into an on order list and so forth. Anything else that we would have put in here, But again, we need to Right now, we just need to check and see if this content even exists so that we can load the proper template. We can figure out what the template is and figure out what content needs loaded and so forth. So with that said, then we come down here. The 1st 1 that we need is we need one called is Page is going to do protected function is page and we need the slug. And then again, we need to create a new we need to create a another Ah, temper. Another template file in our live here. So we're going to create one. Let me just double check this one real quick. So we need to create one that will give us Ah, the content path that would that we want. So we're going to create one under lib called Functions. So do new file functions dot PHP and again we'll go to index like this. I'll just start copying this now, since we've done it a few times and up sex, I don't want it there. Want it here. And I just put these in alphabetical order because when they're loaded like this, you know the PHP essentially finds them. It all works fine. So lib functions and then inside of functions dot PHP, we are going to create a function and the name of that function is going to be get content path. Okay. And in that we need the content type and we need the slug. And this is just going to essentially create our path force. We're gonna set path equal to Sprint F, and our format will be our first place holder. And then we'll do slash content slash Then we'll do our second place holder slash and our third place holder. And then what we're gonna put in those placeholders are absolute path for the first ones that gives us our absolute path and then slash content slash. And then our next thing is content type. So if it's a post is gonna be slash post if it's page slash page etcetera So content type and then our slug it will be, what, post what page? And then we return path. Okay, so that's gonna get us our content path. And then with that, we can come over to our is page method here, and we can do path is equal to get content path. And we were checking for a page, and the page we're checking for is whatever the slug of the page. That request was requested is so that's going to give us our path to. So this would be something like It would be like Slat would be Rourke content page. And then, if about was what was requested. It would be about or for was home. Or if it's the 404 Whatever. Remember, we set our slug home 404 category dash, and we'll talk about category, minute, etcetera, whatever that is. That's what that's what this slug is going to be. And so it's going to check to see if that slug exists in our page folder here, and thus is a page. So this is how we're figuring out the template type. So from that we're going to do if, ah, file exists, which is a PHP function then and we're gonna pass in our path. And then if that if it is, then we're gonna return true. And if it's not, then we're going to return fourth. Okay, so that's gonna be our is page check. We're gonna pass in the slug, and it's going to see if that slug actually exists in our page. Ah, folder here. Now four is post. It's actually the exact same thing Except now it's just instead of ah is Page. It's, of course, is Post. And instead of here looking in the page folder, we're gonna look in the post folder. So those two functions are methods are very, very are very similar. The one that's a little bit different is going to be is category again, we're gonna pass on our slug, and now here we have to do a little bit different because remember categories, the slug we return was category dash. So the first thing that we need to do is we need to split that apart. So we're going to parts equal explode, and we're going to explode on that dash. So if there's a dash is gonna break it apart by that, and then we're going to do slug. Okay, Now, remember, this is a check, So when a page is loaded, you know, it's it goes to pages and post first, but potentially you could have something coming through. This check that isn't form isn't a category, so we still have to sort of handle it in a way where we we recognize that. So we're gonna split it on that dash, and we're going to set slug equal to a ray. Ah, pop and parts were gonna pop off the last part of that. So if we have a valid category category dash, what we end up with with parts is we end up with two UN array with two elements. The first element is going the values gonna be category the word category because his category dash and then the 2nd 1 is going to be the name of the category, whatever it is. Demo, etcetera. So when we do a ray pop word were essentially taking that second element, which is the name of the category. So if we have a valid category, this should be the name of the category. So that's what we want to check. We want to see if that exists in our categories array there in our categories that we designated in our folder. So the first check we're going to do is we're essentially going to see if categories is empty. So if we haven't said any categories, then it can't be a valid category because we don't have any. So if categories is not empty and also not empty slug So if slug is an empty than we're going to do this. If they're both of both, those things are empty. Then we're just gonna come down here and we're going to return false, right? Cause of both those things or either one of those things is empty than it's gonna skip this block altogether and just return false. Okay, so now what we need to do is we need to cats equal array keys, and we're gonna run that on this categories. And what this is going to do is, if you remember, or categories object here. These are the keys, demo and test etcetera. So when we were on a ray keys what that gives us, it gives us an array with just those keys where you have two elements. Want to be the value be demo wannabe test? We need that so that we can run a proper if, Ah, a conditional statement off of in array. So that's what we're gonna do now We're gonna do if in array and we're going to check for our slug if it is in that cats array that we just created with a ray keys. Okay, If it is, then we're gonna set this slug equal to slug, and we're going to return True. Now, why do we do that? Because remember, our slug before was category dash demo. We did that so we could come here and split it apart. Know that we needed the template category, but also still know what category we needed. So now we know that this is a category if it all these checks have happened So now we can slip set this slug equal to the name of the category, not category Dash. Just the name of the category because we now know our template type. Okay, so that's what Ah, those were the three methods we need. And that's what ah, runs here and then for come up here. Let's just do Ah, echo, echo this. Ah, template type. All right. And if everything worked out, well, then we should be able to see what our template type is. So we're on the about page and it says the template type is Paige. Now, if I went to a page that doesn't exist like doggy, well, that page doesn't exist, OK? And now it's returning page, because if you come over here, you remember 40 forests a page. Okay. And it's just Ah, let's do this. This will make it a little bit more clear, I think. Echo this dot template type dot dash. Well, that's fine. That don't matter. And then we'll do this slug. Okay, so the template type and then the slug and this is page equals 404 If we go back to about we do page equals about if we do category slash demo, which is one that exists category equals demo. If we do category dog. Well, that category doesn't exist. Soto for four. If we just do something, let's do Laura. Um, Ipsum Post equals Laure MIPs. Unless do Laura MIPs some gobbledy goop which doesn't exist 404 And if we go to our home page, you see home Eagles home. So home page of page template, his home and the pages home. So what that does is now we know the template tight we need, and we know what content foul we need to load were correctly. We are correctly finding whether something is at 404 if the Post actually exist. If the page actually exists, the category actually exists. So we've got the foundation of what we need now in order to bring all this together, pulling the content, pulling the template and feed that calm content into the template. And that's what we're going to do as we go forward here. 6. Parsing Markdown and Building the Content Object: Now what we can do is we can create our last method in our class here, So we're going to do protected and function, and it's going to be a function called DuPage. Our method called DuPage. And we're gonna head on over to index dot PHP And this is actually the function that we're going to call here. So we're just going to something like Rourke and then DuPage and that outside of any more, any more files that we include here, that's gonna be it, really, For our index dot PHP page because everything we're gonna need from that class is going to be, Ah, it's going to be set up here. And from there it's just a matter of building out the template because again, we we now have our content that were checking to see if he exists. And we're pulling those objects from those files from our post or page or category, whatever. We're pulling those, and then we're now going to inside of DuPage. We're actually going to load our template. So that's all that the roar class that we needed to do everything else is going to happen inside of the template now this is? Ah, a little tricky. Before we get into this, we actually need to create a new ah new template file or a new functions file over here. So we're gonna create a new file. Gonna call this one post dot PHP. This is gonna have everything. All the tag template tags related to working with posts and so forth. For now, we are going to create a function called Get Post because that's what we're going to We're going to create one called get post them or create another actually, tempt functions file called Page, and we'll do get page inside of that. But for to start off with this post one, we're gonna open up our PHP tags and we're gonna get one called Get Post, and I need to get function, get post, and then what we're gonna pass into this is the slug of the post, Okay? And the first thing that we need to do is we need to get the path to the post, so we're gonna do path equals, and we can use our get content path function that we've already created. We're gonna we want a post, and we're going to pass in our slug that was passed in that will get us the past, the the D path. We're gonna do a quick check to see if the file exists if it does not exist. So if that path doesn't exist that we're going to simply return false so that we can use that for checking and other places if we want to. And then we are going to we're gonna create this variable called marked down. We're gonna set that equal to file, get contents, and we're going to pass in our path. And that is going This function is going to then when we pass in ah slug of, say, Laura mdash ipsum it's gonna go in and grab that check of his exist is gonna grab it and set Ah, this variable equal to whatever is in that file. So are it's, um, Lauren file. It'll be this content right here and then from there. What we need to do ah, is we need to parse this and do an initial parts on this. So essentially we need to separate the the Jason from the markdown. And so we're going to use a function we're gonna set post equal to when you use a function called Parse and we're gonna pat pass in are marked down. Now, this could be This is where things start to get a little bit more involved because we need to start now. We need to sort of hook in to our Parsons. So the first thing that we're gonna do is in our class folder. We're going to add a new folder here. I'm gonna drag this up here, but it's going to be this parse down class now, this is not something. Let me just open this up real quick. This is not something that I've written. This is a class that it is out there. I'm showing this to you real quick because if you look at this, you'll see just how involved all of this is. Okay. And the point is, is you don't need You don't need to write this. I didn't write this. There are tons of these classes out there. They all work a different way, and really there's no need to reinvent the wheel. I think when it comes to something, a sort of foundational is parsing mark down. So we're going to just go ahead and use this class. But we still need to. There's some things that we need to do, sort of on top of that. So the first thing that we need to do is we need to go back over to our index file, and we need to include our parts down class. So I'm gonna put that here right in front of our Rourke. Include. And again, we're just using dirt here and we are going to do live and we're going to class, and then we're going to parse down and parts down dot PHP. Okay, so that's going to get the class included. And then the next thing that we need to do is we need to create another function file called Parsa. And on a second here, so in this we need to write are parsing function. So if we come over here to live, will create a new file and we'll create parse er dot PHP and we'll come over here and we'll go ahead and actually do both Parcel and post live slash parsa dot PHP And we'll do require wants dir and we'll do live and post up PHP. Okay, so that that gets those 22 files that we just included. And then if we open parse er the first thing that we need to do is we need to split the document, cause as I mentioned the if you look at this file, if some warm you got Jason and then you've got marked down, and this is what sort of separates them. We've used this special marker and here to separate them. So now we need to actually go and do that. So we're going to create a function called Split Document, and we're gonna pass into that raw underscored document. And then what we need is we need a pattern to To split this with now me personally rejects is something that I'm not the greatest at, but fortunately, thes parses come with Ah, you know, they sort of tell you what to use for them. And so this is the pattern. Essentially, what that's going to look for is it's going to look for those three equal signs that we have here, and it's going to split the document based off of that. So, um, I'm not gonna go to into explaining the pattern there. That's what that pattern does. Ah, and we can just go ahead and use that. So Well, then we're going to use Preg split, and we're going to pass in the pattern we're going to split on. And then what? We're going to split, which is the raw document. Okay, so this is our splitting function. But that's not quite everything that we need. Ah, at this point. And so we're also going to ah, right, a function called parse. And in that we're going to pass in our mark down like we saw before. And with that, we're going to split this based off of the function we just created. So parts equal split document, and into that, we're gonna pass our mark down. And then from there we need we'll create a function here in a second. But we're gonna set result equal to parse metta, which is a function we're going to create and thats parts zero. So the metadata gets sort of stored is the first part. And then the in and from our file, the metadata the Jason gets stored is the first part. And then the markdown gets stored as the second part. So we need to sort of deal with. Both, however, were not going to parse the content yet. And the reason why we're not going to do that as we still need to pass around through some other functions as you'll see here. And so we don't wanna have be passing around with already parsed. We're gonna parse it right before Ah, we display it on the page, which I think is the best way to go about it. In case there were some things you wanted to do with the markdown specifically along the way. You could do that, but generally you want to try and parse or escape or anything like that as close to when it's going to be output to the page as possible. So that's what we're going to do here. So for right now, we're just going to set result when we do result and this parts made a method, what is going to give us back is is going to essentially turn this into a PHP object. And so all we're gonna do is we're going to take are marked down. That was also generated, and we're gonna set that equal to result content. So said that equal two parts and then one. Because that's the second element in our array here. And then we're going to return that result. So and then let's go ahead and write our parts meta function real quickly. Actually, this we could maybe get away with Not well, we definitely at this point could get away with not actually having this function and just calling, uh, because we're just gonna return. Jason decode. We could just call that directly. But I've made it a function because there may be other things in the future that we want to do here, and so we're sort of set up for that. Okay, So essentially, this this function is going to split the document into one element going and create a new array that has one element that has all of, ah, the metadata, the Jason object, and then another one that has are marked down. And then we're going to parse. We're going to Jason to code the metadata and create a PHP object. And then we're going to take the marked down and add that to that object, and then we're going to return the entire object. So when we look at this parse method here. This parts function here that that we're going to be using inside of our get post here, that's what it does. Okay. And then inside of get post Here, Once we've done all that, then we can simply return post. Okay, Now, the reason we need that is because if we come over to our Rourke here, we are going to use that inside of our DuPage Ah, method here. So inside a DuPage, we're gonna we're gonna do start off by globalizing a variable called Page. Now that doesn't exist yet. But once it exists, it will now exist in the global space. And we need that because we're going to be including template files and so forth. And this page variable is going to be the variable that holds all of our data about the page that were on. So it's gonna hold the title, the author, the content, all of that stuff that we're going to be using inside of our template. So we needed in the global space. We're gonna globalize it here. And then again, we have just a big sort of switch. Ah, function, hair switch statement. So we're going to do? Switch this based off of the template type, which is the thing that we just figured out so again, up here, we just figured out the template type. Now we're using that template type, and we're gonna run this big switch method or this big switch function off of that. And so we're gonna have several cases here. The 1st 1 we're gonna go through is gonna be case category now categories air a little different because categories are all stored in one file here. We're not. We're sort of not pulling in that data from a pig from separate files and so forth. It's all in one file. Ah, and so we have to do this a little bit differently on because with categories were also going to display, we're gonna splayed information about the category. But that's not really the big thing about categories. The big thing about categories is the posts that are in them. And so again, it's just a little bit different. So the first thing we need to do is figure out OK. What? We need to grab our category. Now remember, from our categories here, this is a Jason object that ultimately through our get categories, function gets turned into a PHP object. And what we have available in this categories appear is a PHP object. So we have a PHP object with all the categories that we've created. Well, we just want the specific one that we're after. So we're gonna set category equal to and it's gonna be this categories and then we're going to the key that we want is whatever our slug is right. Because remember, we we now changed our slug up here with is category We set this slug equal to what we parsed out is our slug here. That's why we did that. So now we can use that over here. So that's gonna grab us the specific category that we're after from our categories. Object. And then now we can use that to create our page information that our template needs. We're gonna see Page equals new, and we're just gonna do standard class like this. And then we're just gonna explicitly settle these page title equals in this. In this case, the page title be the category name and the page content will be the category content. And then the page posts in which this is unique to ah two categories that's gonna be equal to category and then posts Right and again, These are all these are all properties of the object that we created over here. So post names Locanda etcetera. All right, so that's all we're referencing over here. Okay, So that essentially what this is doing is it's taking our category information, and it's putting it into this page object which we globalized up here, which we're going to use to populate data inside of our template. Okay, so the reason that we've sort of we've moved it from category sort of a category context to a page context is simply because we're using all of the eventually we're going to use all of the same functions, and they're going to reference this page. Ah, object here. And so we need to just build it the same for every category or a page or ah, 404 post etcetera. All that needs to be the same. So those functions can reference the same object, and that will start to make a little bit more sense. As we go through this. The next one is an easy one. So we're going to do case are assess. And in this case, we're just gonna break right, so we don't need to do anything with it. Um, as a matter of fact, you know, I guess we probably don't even need this in here, to be honest, because we're not doing anything with it anyway. So, um, the RSS is gonna be handled completely by the template file that we're gonna create for that. And we'll sort of get to that here after a bit. Um, on. Actually, actually, we do need this now. I think about it because we want to keep it from going down to the default. If we are on the RSS feed, right, because otherwise what we set for the default will it'll if we don't put this in here. If you put this in here, it'll just It'll run this case and I'll just Brill do nothing. But if we don't put it in there to keep going and go down to the full, and that actually isn't gonna work for us, we actually do need that in there. And then the next one is the home page. So if if the template type is home, then this is where and actually we need to Let's go ahead and create, um, our post or page file here because we need our page followed. This points, we're going to do page dot PHP. This is where my story or functions related to pages will come over here to index. And we can copy this one and just change this two page dot PHP And then if we take a look at ah, If if we look at that, then we can we open up page dot PHP We can essentially do the thing. Same thing we did here with our pope get post. But now we just changed this to get page. So get page and we change this two page. Okay, so it zvehr actually the same function. But these air helper functions. So it's OK if you're repeating stuff a little bit here, Um, because the whole point of them is to make template ing easier. It's not necessarily to make you not initially focused so much on efficiency. Here. You're more focused on ease of use when you get to building the template. Okay, so make sure and say this and then inside of Rourke under case home. Then what we can do if you remember. Here it's get page, and then we need to pass in the slug. So for on, we know we're on the home page. Ah, and so we are going to do mark down and actually, no weaken, just weaken. Just go. Um, Page page equals get page, and it's going to be home like this. Okay, And then break and then case four page is gonna be very similar. It's gonna be page equals Get page, and then it's going to be, Ah, this slugs. We're gonna pass in the slug and break, and then we're going to do the case for posts. So if it's a post, then we're gonna do page equals. And this time we're gonna do get post, and we're gonna pass in this slug on break, and then the final case is going to be default. And that's if none of the others are met and this is going to be page equals and it's gonna be get page and it's gonna be the 404 Okay, So just to recap here, we've got our template type up here we set our template type here in set template. We know what that is. Now we're calling DuPage, and in that we're switching off of that template type. If it's a category, were grabbing the category and the building our page variable or page object manually. If it's RSS we're doing nothing. If it is the home page were calling Get page for the home page. If it's a page we're calling, get paid for the specific slug that was passed, It's a Post were calling Get post for the slug that was passed. And if none of those air met, we're getting we're calling Get page for the 404 and again. Yet Page is going to get the path to that particular piece of content. Check of it exists. Parse the mark down and then return ultimately, Ah, the object here And I guess we can do page and we can do page. Since this is get Page makes a little more sense contextually and so again, this is what DuPage is going to dio now. The last thing that we need to do is we need to load our template, but before we can load our template. Ah, we need to actually have template files. So the next thing that we're going to do is we're not going to create all of those we're going to create sort of the placeholders forum. So we're gonna sort of close these up here, and we're creating a new folder here, and we're gonna call this templates and then inside that now, this is going to work where you can create I'm gonna create one called default. You could create this like you could create, say, a WordPress theme or whatever. And you could switch him as long as you've written the templates there, There they exist, etcetera. You can do that. Okay, so, um, that's how this is going toe work. One thing we need to do now that we've set that is we do need to set whatever the template iss we're gonna set protected, and we're going to do template equals default. So now, if you wanted to switch your template, you would, of course, build the template files inside of a folder and that this templates folder. But then you would also switch this out. So if you made one called now, uh, portfolio, you would change this to portfolio, so we would pull those files instead. OK, but in this case, we're just going to use default now, inside a default, we have, ah, number of template files here again, we're just gonna create placeholders. So we're just gonna create a bunch of files, so we're going to do for four dot PHP. Ah, let's do category dot PHP Another one is home dot PHP. Then we're going to have page dot PHP and we will have post up PHP. And a little bit later, we will get to our assess dot PHP. Okay, so those are the different templates that we have at this point, we will have some other template files, but those are gonna be more helpful helper files now, just so we can see what's going on here. I'm gonna put the name of the template in each one of these files. So 404 here we have category. Ah, here, here we have home. We'll save that. Here we have page and post and or assess. Okay. So if we load these files will be able to tell, I'm gonna can close all these to clean up this up just a little bit, but again, if we go back to index, stop PHP where In San? Saying new. It's it's instance of Rourke and they were calling this Duke DuPage method inside of Rourke were figuring out what our slug is from that we're getting our template, and then we are coming down here. We're building our page object that we need to populate the templates. And then, like I said, the last thing we need to do is we need to load the template. So after we this switch a statement here, we need Teoh. We're going to then call a method called this load template, which we haven't written. But that's what we're gonna write right now. So we're gonna say protected and function load template and we'll load. Template is, do Is, is essentially we're going to do two things. So we're gonna globalize template type, and we're gonna coat globalize something called page slug again. These are template type exists, obviously, but page slug doesn't. But these are again putting these into the global space so that they can be used by the template files. That's why we're globalizing. This so ready set. Paige slug equal to this slug. And then we are going to set ah, template type equal to this template type again that puts those into the global space. So now the slug and the template type will be able be available in the global space of the template files can use them. I'll show you where that comes into play. Ah, little bit later. But the big thing that we're after for this method to do is we need the template path. Um, so we're going to do template, template, path equals and we're going to do ah, sprint F on this, and so our format is going to be are our first place holder. And there's gonna be let me first place holder asking me slash templates. That's the folder where templates are loaded and then it's going to be our second place holder and then a slash and then our third place holder and then dot PHP. And what we're going to populate that with is our absolute path. Okay, so that's what's gonna go in the first place holder. So it's going to be asked absolute path and then slash templates, and then we need to Ah we need this template. So we need which template we're going to use. And then we need this template type is what we're after here, Okay? And then we're going to include that template path. We're going to include that template file. Hey, but let's just let's I'm gonna comment this out for a second, so you can see this path row quick. So let's go echo template path so you can see what this path actually looks like. All this is doing is building a path to a file, and so were we use this template Because, remember, we have this template here. We set this template to default appear. So that's gonna drop that into the URL or to the path our template type is whichever one of these is, and then we're gonna add dot PHP to the end of it. So if everything went right here, we should get the that path echoed out to us here, um, call to protecting method DuPage. So I said I'm a DuPage protected, but we're calling it in a global space, so I need to set dupage to public, okay, see what that gives us. And so now you can see the path home a bun to workspace Rourke templates default home dot PHP. Okay, And if we we go to about it's page dot PHP If we go to warm ipsum its post dot PHP These air, the right template paths here. Um, if we go to category as slash demo and then it's category dot PHP. Okay, so that's that's what this is all doing. And then, of course, this include is actually going to include those files. Let me just figure out where I'm printing out. Maybe I'm echoing all right, so we can get rid of this right here. Since we've sort of worked through all this, we'll get rid of this. So now we're actually including the template file. So what we should see is those those names I put into those template files so category if I go, if I just go home, got home? If I go to Laura, um, Ipsum, I'm getting post. If I go to some crazy gobbledygook, I should get page, but it's for a four page, um, etcetera. So now that's that's what this all does is it allows us, and then we can And actually, if we go to? Let's just go toe. Ah, page dot PHP Let's go to this template file. If I could get it to open, we can set a doing page. We could do PHP global that page variable we created the global space and weaken do print are and we can do page and this should give us the information about that particular page. So if I go to about you can see we get the object we get title slug. Author date content, etcetera And now all we need to do is build our template files and populate those template files with this data. So that's what this allows us all to get to at this point. And we're getting to the point now where really we can just sort of break down to, um to creating our actual template files, which is what we're going to do in the rest. Of course, 7. Create the header.php Template Part: All right, So now let's go ahead and move in to start building some of our template files, and I'm gonna close up a few of these things in here. Just toe, give us Ah, a little bit of room to work with here, and we'll open this stuff up sort of back as we need it. But we're going to start. Wouldn't come into templates here under the fall. And what we're going to create is we're going to do create a header dot ph pieces is a template part. This isn't a page in it of itself, but it's gonna help us to build our template pages. We'll go ahead and open that up. And so this is This is where you have some options when you're building your template. Now I am using for this I am using bootstrap, and I'm using sort of a combination of this Blawg theme here her, this blogger example and this carousel example. So from carousel, I'm early. Just using the menu appear because I like that menu and from blogger I'm basically using everything else. Okay, so it's sort of a mis mash of stuff the way it works, though is that because this is all bootstrap. I actually just took the style sheet from this blawg example, and that's what I'm using over here. And then you could just change the way that it is constructed in the menu is constructed in the HTML, and that's actually all of the stuff to do. This is built into bootstrap. So I didn't have to I didn't really need this particular ah style sheet from this example. So depending on what you want to dio again with with how you build your template and so forth, this will certainly change for you. I'm gonna try and streamline this a little bit because there's if you've ever worked with Bootstrap. You know how it works. There could be a ton of classes and stuff like that. So I'm gonna go ahead and drop in just the head tag for right now and sort of just walk through this. So this is a standard sort of bootstrap head tag that you will get from any of their examples. So of course, we open the HTML tag delaying languages, English. We opened the head tag we have are required meta tags, which the character said, is set to utf eight and then we have this meta tag here which essentially is for responsive sites. So we do that. We include the bootstrap CSS right here. Notice we are using the cdn so that give us a little bit of, well, help us tap into the browser cashing that will get from that. And then we are referencing this blog's theme that we've got from bootstraps, so that doesn't actually exist yet. So what we want to do is won't want to go into default and we'll creating new folder and we'll call the CSS. And then I will get the file that I have from downloading that template from or that that style sheet from bootstraps, ITT's BS Dash blog's so now that exists. So we're just loading that and I'll come back and we'll talk about thes PHP functions here in just a second, but I want to go through and get everything sort of set up in here. So the next thing is a Nikon font. I chose one called open iconic. It's, I think, one of the ones that ah bootstrap recommends for use with it, and so again, I'll just sort of dragged this over here under the default template. And if we open this up, you can see it's the open, iconic folder that you'll get from downloading from there. And this is really just the full download. So has the CSS files, the fonts and so forth. What we actually load in our page is the under the CSS folder in this open iconic, we're going to load the open iconic bootstrapped out CSS. He could probably clean this up. Some of the stuff you probably don't need. I would guess you could probably use the men ified versions. Maybe so, um, but, you know, this is just sort of what we're loading for now, so feel free to change that as you see fit. And then the last one is this style dot C S s. And this is sort of just going to be our custom styles here, and I'll go ahead and dragged this file over here and open it up real quick and you can see it's not a ton. We will sort of go through this as we get to those different points in here. But this is really just overriding or changing anything that's in bootstrap end in this bootstrap example. Download here. Now, this is where this particular sort of framework having a framework like this can be really powerful because again, you can create multiple templates here. And you can look at different examples from bootstrap or whatever you want to use. That's out there for, Ah, front and frameworks, and you can really pretty easily integrate them into ah, what we're doing here. So having all of this sort of back and support in the PHP code helps us to more easily be able to build our themes. Okay, so that is the header. Now, let's talk about these these functions here. So the 1st 1 that we have here is home. You are l and home. U R l is one that's just going to help us very quickly. Get the home you Earl of Of of the site. So but there are a couple things that we need to do here. So first we need to open our conflict file back up and inside of our conflict file. We need to specify what's called an absolute you, Earl. And so let me go ahead and just open this up, Earl, Quick. So we're going to use this. Define again. So we're gonna get you define and we're gonna do abs your l and then we're going to put in the u R l Ah, wherever this is installed. So this is similar to like when you install WordPress, for example, it figures out what your site you are. L is. And it say's that saves that as an option in your database. This is sort of the same thing. Ah, we're just setting it up inside of a conflict file here, since we don't have any database. So that gives us our absolute you, Earl. And then from there we can get our we can write our home mural. Ah, function here. So what we're gonna do is we're gonna go over under lib here again, and we are going to create a new file and we are going to call this template dot PHP And let's open up Index and make sure we get template dot PHP required in properly again. This is sort of why I like putting these all right on the front in one spot here, instead of putting them throughout the files It's a lot easier to keep track with if you're not gonna auto load. All right, so that gets that included. And then if we go into and we open up ah, template dot PHP Our home your l function is actually pretty simple. So function home you are l And we're just going to return our abs. You, Earl, are absolutely Earl. So that's all that that function is, um, reason we put it in the function. It's a little cleaner in the CO. But also, there may be later on where you want to do something with this girl. And so if you just put it in the function from the start, then you can sort of mess with it inside of the function if you want to later. And you don't have to go back and change all of the places where if you just you just called absolute. You are l directly you don't have any sort of buffer. So this function access sort of a buffer in case you want to change things later. Right? So there's that that gets all of these home your l's working, and then you can see we have this one here called title Tags. So this is also under inside template dot PHP immune to function title tag. And we're going to Ah, globalize page or template type. You remember we we made that available. We globalized inside of our Rourke class. So that would be veil available in the global spoke scopes and how we can globalize and use that here, and we know what our template type is. And so then we're gonna start in array. It's gonna be tight. Ah, title parts. And it's just gonna be an empty array to start off this deal with any notices and then we're going to check. And where? If template type is equal to a category, then we're gonna build the title tag a little bit differently. So what we're gonna add is gonna add title parts, and this will Essentially, what these these brackets here do is we have an empty really started here. This will simply add to it. So it'll basically add an element to this. And so we're gonna set that equal to, and we're gonna have category colon and then space. So essentially, what that does is if it's a category, the title dak Tag will say category Colon, and then we'll get the name of the category. Okay, so that's just Ah, quick check that we're gonna add if that happens. If it happens to be a category page, then from there for everything we're gonna do title parts, we're gonna add another element, and we're going to set this equal to something called Get the Title. Okay, Now get the title. If ah is one that's going to, as as it describes is going to look in the page object and actually get the title. Now, we've already created the file where this one goes, so that's under functions and so up above get content path here, we're going to do function, get the title. And here we are going to Ah, actually, we need to set this object equals No, that will make a little bit more sense later. But the basically the idea is is you can either pass in a post up, uh, on object that has the data in it. Or you can grab the what's in the global spoke space inside of a page. So it allows you to, um that allows you to create by putting this. Using this object equals an old thing. It allows you to create ah, function that can be used. You can create sort of nested loops essentially, because you can use what's in the global space or you can pass in your own, and all these will still work again. Like I said, that'll make a little bit since since a little bit later when we get into some or the template here. But we do this for all of these, so we're gonna see if object is equal to know. Then we are going to globalize our page variable again. That's the one we set inside of Rourke that we could that we spent that all that time creating inside of Rourke to make available to these templates were now grabbing that. And then we're simply going to return page title. Now the thing is, is you could do this all inside of just header dot PHP, but that's a lot of extra code to put over there. You want to use this little code over there as possible, so we put these inside of ah functions file so that we can just grab these functions and use them. OK, so it helps make this all a little bit more reusable. And then if object is not equal to know, that means an object has been passed in. And so we're just gonna return whatever the title was for the object that was passed in. Okay, So again, that allows us to create these the sort of nested loops. And when we create our category templates specifically is one that will and our home page template. That's when we'll really need those. Okay, so that's what get the title does is essentially gets the title. Ah, as the name suggests. And then if we come back over to template now that get the title works then Ah, we set title, uh, said Title equal to And we're gonna implode the ray we just built. So we just build an array of parts. Now we're gonna implode it into a string which will go ahead and create the title for us. And then we're gonna echo title. Okay, so now let's let's ah, look about making this work. So let's go to let's open up page dot PHP And let's just real quick changes to include header dot PHP Okay, so it's just including our header inside of our header. We've got home, you Earl, and we've got our title tag here. Ah, and so if we go to a page, hopefully this will work. You can see up. Here are title tag now says about Let's go ahead and just view page source here. You can see what we just created. This tag here, you can see under where we're referencing home your l it put in the one that we specified in conflict dot PHP So this links everything up properly. You could see our title tag here is set up properly as well. So that's what uh, those those functions do allows us to ease all of that and make it all that just a little bit easier to create. All right, so now we want to go back over to header, and we want to finish building this out. So again, I'm gonna sort of speed up some of you having you watch me type a bunch of html because again the HTML here is is sort of arbitrary, right? It's whatever you want to build for your template, so that it's a lot less important than the PHP Parts of this is just what I really want to focus on. So this again is just from bootstrap. So whatever you're using, you would sort of build your header here. Um, you can see we're still using home your URL, homegirl. Now, we've got this one called get site info that we need to build. And we have another one called knave menu that will need to build. Okay, so we got our knave class. We've got our brand here, which is referencing our home. You are? L Then we have this. Ah, echoing Get site info. So what is that? Well, get site info is again inside of our functions here, and we'll go ahead and place this above get the title. So will do function, get site info and will set ah, item equal to know. And then we're going to switch off of Ah, that item. Okay, so we're gonna run a switch statement, and so do switch item, and then we have we're gonna have two different cases that we support, essentially in a default. So we'll do case. And if it's what's passed in for the item is title, then we're going to return a constant that we haven't created yet called site title. But we'll go create that in just a second break. And then if the case is description, then we'll return again. Another constantly haven't created yet called site description. Uh, and we'll break. And then the default I've decided for default to just default to the site title. Um, you could sort of You could handle that however you want, but I'm just going to do site title here. Okay, so that's what gets site info does. Obviously, the more complex this gets, you might add to this. You may have more than just title in description and so forth. But again, we need to come over to our conficker, and we need to then actually create those constants. So we'll do define and we'll do sate title. And for this I'm just using The title is Rourke, and then we'll do define and site description. And for this I have this line. I use a blogging ish PHP thing. That's what this is. All right, so those are our constant. So now, over inside of blues functions. Uh, now this this will work so over here inside of Header. This will output. Remember, we didn't pass anything in here. We could make this explicit and do title just so we know what's actually being displayed here. So we'll go ahead and do that. But that's going to display the site title here in this knave bar area. OK, so that's what that is. And then we get into our actual knave menu that we're gonna create here on then here is a link to our RSS feed. So just home you are l slash feet. So let's talk about ah, knave menu for a second. So first we need to create a new file inside of lib and let me close this up. We need to create a new file inside of lib, and that is going to be called knave. Actually, when you cook justice, call it menus dot PHP Okay, and then we need to go back over to index and we have a new file we need to include, So we're gonna do Ah, I'm again. I'm putting these in alphabetical order. So will do menus dot PHP. So that's included in And so now we have the menu. So let's go ahead and open that up and again. A little bit of this is going to be arbitrary, but ah, because it's going to be based off of the HTML for the template that using. So if using bootstrap or something like that, or you do that using a different bootstrap example, it's gonna be different that each smell, it's gonna be different. But again, the PHP code is going to be sort of very, very similar. The other thing that we need to do is we actually need to go into our content section, and we are going to create different menu. So if we go up in the content and let's go ahead and getting close, some of these up let's go into will create a new folder and we'll call this menu. And then inside of this, we can create different menus that we can load. So in this case will create one called Primary and Primary is just going to be a Jason object. So, um and actually, let me go ahead and just let me delete this one. Let me just drag over the one I already have. So let's go menu. Let's open this up So it's just a Jason object and you can see it's really simple. This is the menu items. So the 1st 1 is home and then about, and that's what's gonna be in our menu. So ah, here you have the slug the path. And then here you have the name. What, you want the name of that item to be. So in this case, they're both. They both have the same name and so forth. So that gives us our menu. This is inside of content against you can maybe guess what we're going to do with this, but in our knave menu function, so we'll create function, have menu, and you have to pass in the name of a particular menu. And then the first thing that we need to do is we need to get the actual we need to get the file that we just created. So that menu file this primary file here. So we already have the function to be able to do that. So I'm gonna create a variable called menu raw. It's going to be set equal to file, get contents and then get content, path, get content, path and the path is going to be menu. All right, so that's gonna get us to the menu folder, and then the menu that we want is whichever one was passed in. Okay, so that's going if the menu exists, that's going to ah, give us the menu. Now, you probably want to do a check here That says is something like this. If not menu. Well, then we'll do Just return. Are we Could dio return false. Okay, so if the menu doesn't exist, we're not gonna do anything. And then from there, we can go ahead and we can create our menu items. And we're used Jason to code because that's bringing in that file, which was a Jason object and will pass in menu raw that we just grabbed. And then now we can start building our menu, and this is where it gets a little bit arbitrary because it depends on what sort of theme you're building here. But I'll show you what I've built. So we'll set menu equal to, and we're gonna create an UN ordered list. Class equals knave bar. Ah, knave, M r auto. We'll close that up. All right, We'll close that up. Right so that starts our menu. And then I'm gonna come down here just so I don't forget. And we are going to do menu dot equals and will close up our menu. Now I'm keeping this knave menu isolated specifically just to the menu list. And then if you have other HTML in your theme that wraps around this, which we will I'm putting that in the actual header file to make this just a little bit more reasonable. So hopefully you don't have to change this stuff as much as you might have to if I put all of that code inside of here. All right, so now that we have that done now we just need toe looped through our object and we need to build our menu. So we're going to do for each and we'll do Ah, the menu items as slug and then title. So that will put these Ah, we can use slug entitle inside of our code, going forward to reference thes and then the u r l is going to be equal to, and we're going to use Ah coronary here. Now, this is a little complicated. So, um, I want to actually just sort of dropped this in and then explain it to you instead of typing through it, cause I think be a little confusing. Right, So these parentheses here are just encapsulating veterinary, okay? And then this code inside is actually the coronary. So the condition we're checking is if slug is equal to home. Okay, So if it's equal toe home, then we're going to use home. You are l and that's it. Otherwise, if the slug is not equal to home, we're going to do Sprint F, and we're going to use home your l slash the slug. So essentially what this does is it constructs the u. R l and says if we're on the home page, we just want the whole mural. We don't need the slug to use the slug it all. But if we're on any other page, like the about page or anything else that we might want to lean too, then we need to upend the slug to the end of it. So if slug is equal to home, you the true statement comes first. We're just going to use home your l than the colon. And this is the false statement. So if it's not equal to home, then we're gonna build a girl that has home your l slash slug. So that's what we're doing here. So we're building the girl first, and then we are going to add to our menu. So this is going to equal sprint F and we're going to build ah, list item inside of this the classes again. This is specific to bootstrap and to the particular ah theme or example that I happen to be using these could be anything a class equals and we're going to eat, use knave, dash link and then a draft equals and our first place holder. And then we'll close that up. We'll do our second place holder and then we'll close up, are a tag and close up our list item here and then here. We need to specify for a placeholder. So the first place holder is the URL, and the 2nd 1 is the title. If you're not familiar with sprint, half I would suggest getting for smoke. Familiar whisper in Africa Looking up the man it's really handy function allows you to create a format. You can drop the's placeholders in here these percentage esses and then these. These variables will get dropped into these spots here. So it's a really handy way for writing HTML that's clean, that I like to use a lot. You'll see. I use a lot throughout this. Okay, so that creates for each bar menu items that's going to create each of those items and append them to our menu. And then we close up our menu here on The last thing that we need to do is then echo the menu. If we go back over the header, that's our knave menu right here. We're referencing primary. Let's see if everything worked out over here and let's see, let's inspect every page source, see what this full areas function. Nobody classes. We still need to create body classes. So if we come over here with come back over here, we have this one here called Body Classes, which I sort of forgot about. So body classes, I believe is inside of actually is gonna be in two different places. So the first place that is going to be inside of is template, which we already have. So we're gonna go into template, and you do function body classes. And this is just going to echo get body classes, which is a function we haven't created yet, but we are going to Okay, so the point of this is ease of you. This is ease of use inside of your template, but also having get body classes in case you later on, when I add stuff to it Ah, that you can Ah, you can call it directly if you need to. And so forth. If you don't, for some reason, you don't want to echo it right away or whatever. So again, it just creates a little bit of a buffer there for you to be able to work with this template ing stuff. So then we need to go over to functions and actually write to get body classes function. So we're going to function, get body classes, And you remember those two? I told you when we did template type when we globalize them inside of Rourke. Template, type and page slug. This is where we're going to use them. Okay, so that's this Helps us to be ableto create intelligent classes for our body that we can use inside of our CSS So if we're gonna start off with if, ah, neither one of the if one of the other, these is not empty. So if not empty template type or not, Empty page slug. So if ah one of them has any sort of value to it, then we can do something with it. Otherwise, I will just come down here and return false. Okay, so all of our actual functionality is gonna be inside this if block so this won't do anything unless one of those exists and has data in it. Right? So the first thing we're going to, we're going to sort of do like we did before. Ah, with our menu and some of the other stuff where you be building array. And then we use implode to create the string. So that way we can easily add things to it. It's going to say classes is equal to array, but this time we're not going to start off with empty class. We're gonna create a first element. This is class equals because we're not gonna have something because both of these ah, one of the page slug or the template type has data in it. So one of we're gonna grab be grabbing something from one of those. So then we're gonna close this up down here, so classes on and brackets equals and then our double quotes we're gonna close are double quote. So essentially, if you look at this is our opening double quote. This is our closing double quote, and then we're going to build stuff inside of this. I will go inside of that. That class is blocked. That's what we're doing here. So we're gonna look at template type first. So if if template type ah is not empty, and we're checking this again because appear we didn't or statement eso template type could still be empty, and page slug could be the one that has data in it. So we do need to recheck this at this point. So classes were going to our brackets and we're going to set this equal to, and I'm I'm doing it this way. So I'm going to spur naff, and our format is going to be template, dash and then placeholder. And the placeholder is the name of the template type. So what this is going to give us is, let's say we're on the page template. That's the tape page that was loaded. One of the classes in our body will be template dash page. That's what this is building here. Okay, now it's similar for the page slugs. We'll do, if not empty, and we're going to do page slug. So if that's not empty, we're gonna add an element to classes. Ah, and that is going to be equal to, and we'll do sprint off again and or format is going to be page. And I'm just doing page i d could be DuPage slug whatever you want to do. But I'm just going page I d here and then the page slug. So for on the about page, what we'll have is Paige Dash d dash about. So if you look at this, if you're if you're on, let's say the about page. What you're gonna get is two classes in your body template dash ah, page and then page I d. Dash about so you will know in your CSS. If you are on that, you are on the page template, and you're on the about page. That could be handy for getting really group granular with your CSS if you if you want to do that so and again, we just want to support that inside of our code here. Ah, you may never use it, but again, it's supported. So we're going to, in turn implode. Ah, we're not gonna have any sort of delimit er to bring them or glue to bring them back together. And we're going to use our classes array. So this takes all this this array that we built and turns it into a strength. So that's what get body classes does. So let's. And now you can see we have a menu here. Review page source. You see what we just did? Body class, template dash Paige, Paige Dash D Dash about. You can see our menu down here, you else, And we have a list item L A l I class knave item have all of that. Okay. And we come up here, we have our home page that links to the Rourke, the route and this links to work slash about this links to slash work and this links to work slash feed. Okay, so we've got the beginnings of our template coming together here, so that is our ah header file. That's everything that I have in my head or file. And again, you know, we're building the html back and to support the template and we also building the template . But the template can sort of be whatever you want it to be. That's that HTML that you can decide. Ah, how you want that to go together. So, of course, this is just the first template part. This is a header. We still have her footer in our sidebar. Ah, that we need to get to which we're going to get into in the rest, of course. 8. Creating footer.php and Blog Widgets: next template file we're gonna take a look at is footer dot PHP. So again, let me go ahead and close some of this stuff up here so we can kind of focus, but we're going to create footed up PHP, which, inside of our default here we haven't actually created gets We're gonna go ahead and do new file on do footer dot PHP. Go ahead and open that up. No. And again, I'm gonna go ahead and just drop this in over here because this is a lot of HTML, and a small part is a bit irrelevant. This is, quite literally what was given to me from bootstraps. So you can see the bootstrap core down here. You go to the examples on bootstrap all this each tamales here or is there. And so Ah, the only thing I really changed is you'll see here. This is blocked template built by M d o. I left that to credit. Give them credit. But then I added the line the work blawg application built by me and my twitter Ah, address. So added that here and then up here under ah, this section up here called footer Widgets I have these links. Have these four column div tags here and then I have widget latest posts, widget categories and widget text. So you actually can create widgets that you can use in the sidebar and in the footer. And that's what these are S O. But we, of course, need to go through, and we need to actually create all of these functions. So we're gonna go again into lib, and we're gonna go create a new file, and it's gonna be called widgets dot PHP again, we will need to go to index, and we'll need to include this file. So it's available to us, so widgets dot PHP So that's now included. And then inside of actually, go ahead and open up widgets. We're going to create our actual widgets here. And so we've got a few here. The 1st 1 uh, well, actually, we'll go in a little bit different order. Its latest post categories and text. Let's go ahead and go with categories first. So all this is is we create a function called widget categories and we're gonna have a parameter of title so that we can set a custom title. So if we put in the sidebar, we can give it one time. If we put in the footer week, you give it another. But we'll default it to just categories like this. And then we'll go ahead and build our widget. So obviously, we're display a list of categories, and so let's go ahead and get our categories. Now you remember from our category dot PHP file. We already have a function. If I can get it to open here. We already have a function called get categories so we can just call that because that gives us our categories. So we do categories equals get categories and then we'll do a simple check if empty categories, then we'll just return false like this. Okay, And then we just need to loop through and build a list of our categories. So we've sort of done this Ah, a little bit before again. I think Ah, a lot of this is hte amount. So I think I am just gonna drop this in just because you can see it's mostly html. But we start off big, creating our widget who create are starting div tag here just like I've done before. All the other ones. We have our closing dig Div tag down here we have our title. So we're essentially taking title from appear were using Sprint F to drop title into this place holder between two H four tags. And then we have our opening ordered list class here and this These classes are No. This is list on style. Them bees dash zero post list. This is all stuff from bootstrap. This could change if you're not using bootstrap for using something else. Okay, Same with a lot of this down here category list item and so forth. So ah, lot of this is pretty similar to what we've done in its arbitrating could be changed. Then we run our for each loop. So for each categories as category, remember, this is ah, this categories was a Jason. There was a Jason object that we then converted into Ah, a PHP array over here using. That's what we do in here. Jason to code file and true means turn it into an array, not an object. So we're doing that and then Ah, we're just building each one of our list items. So we're using sprint half again. We're creating a list item class category list. I'm on creating. Ah, an anchor tag. First place holders, the URL second place holder is the name of the category. So again, this is all really, really similar to get the girl we're using a function called get category link and then we're passing in the slug and then for the name of the category is just the name from our category array. So get category link is something that we haven't actually built yet. So we'll go ahead and we'll go on build that and to do that will come over here to category and will come down and will create a new function called get category link And what you pass into this is the slug. So we'll do category category underscore slug. So we're gonna pass in the slug of the category and then this This function is really simple. We're going to do return. We're going to sprint f c. I use sprint half a lot. That's why I say it really recommend boning up on it. If you don't know exactly what it is, we're into our first place holder. We're new category slur slash category slash and then our second placeholder And where we're gonna pass in is home you, Earl. And we're gonna pass in arse look, category slug and were just returning that. So what that does is it builds a u r l That is our home. You are l So that's going to essentially link us up to Rourke right here. And then we're doing ah slash category on. That's going to, ah, give us the girl that's going to build that part of it. Because if you remember from the very beginning, rul structurally support his category slash like, demo something like that on actually probably can't see that, but something like this category slash demo. So that's what it's Ah, that's what we're supporting. And so that's the link that we're building so slash category and then slash the slug the name of the category. So in the example, just used to be like slash demo that builds that you're on. We just returned that you are l over here and that builds our category link for us. So that gives us our first widget, which is, um, are category widget. And then if we go back to footer, who see, we have one that's called latest posts and actually may save this real quick. So let's come back over here to widgets will create another one called function widget Latest posts and again, we're going to allow to be able to do a title will set that equal to latest posts. And this one, we're also going to allow the number of posts that we want to show to be specified. I'm gonna set the default to five. You could, of course, set that whatever you want. But you can also pass it in and override it. So the first thing we're gonna do in here is we're going to get our posts or posts equal get posts. Well, we haven't actually written a get posts function yet, so we need to go ahead and write that we're gonna put that in our post op PHP files will go ahead and open that up and we'll put that above Get post. So we'll do function get posts right now. Doesn't take any parameters. And ah, now what we can do is this one's a little bit different, so we're going to do posts. Dir. So we're gonna get the directory of our post, and we're gonna do a sprint half. And we will set that to placeholder and then slash content slash post and then the abs where we could do home. Actually, we need the abs. Paths are going to ABS path. So we need the path. Not to you, Earl. So that gives us our path to our post folder, Right? So under content, we have this post folder right here that gives us the path to that. And then we're going to use this funky little thing here, and I'm gonna drop this in here so we can sort of talk about a little bit more, but you may or may not be familiar with this, but you see, we're doing files or equal to we're going to use a function called a ray filter, and then we're going to use one called Skander. And then we're creating a uh, You could call it an anonymous function, I think closure. Ah, think of earthward Lambda function that has different names. But this is, ah, function that were essentially creating on the fly. And I think it's five point five and greater something along those lines. You can do this inside of PHP. If you've done the sort of Java script, the single look familiar. You do this all the time inside of JavaScript. But now you can do it inside of PHP. And the reason we're doing that is because of Ray Filter. If we just go to array filter. What a ray filter does is if, as it says, filters element elements of an array using a callback function. So it goes through an array, and it basically sends all of though each element through the callback function and the callback function is what actually does all of the work. And so we're just creating that callback function on the fly here, instead of creating it somewhere else in here, and just helps keep from muddying things up a little bit. And what this callback function does is actually pretty simple. So if you come over here, it runs. This PHP method called Is Dir. Well, what that does is it just checks to see if, well, the particular what the particular item that was passed to it is a director or not, And so if it's not, it's basically going to return. Ah, true, or false based off of that. Okay, so that's all we're doing here. But we do have to get a little sort of funky with this a little here because normally you probably have seen this and it doesn't have this part here. If you've ever seen this will just be function item. The problem with that is we need this post directory. We need to use that. And so we need to tell our function we're creating on the fly that were passing in the item . This is the one that is being passed to the callback array for array filter. So that's the one that's gonna be analyzed. But we also need to use post dir here. So we use this when you create these these functions on the fly, you you pass in the item that's that's being looped through by a ray filter that again, a ray filters going to loop through the array that is being passed, which is what we get from Skander and scanning this poster. It's or race an array of all of the items inside of that particular directory. Okay, so if we go to post, it's gonna be ipsum alarm alarm Ipsum. So it's going to pass each one of those to Ah that is going to create an array of those items. And then a ray filter is gonna go through each one of those items, and it's going to look at this right here, and it's going to send that item right here to disfunction. We create on the fly, which is going to check to see if the item is a director or not. But we also need to pass in the full absolute you are. L toe have it checked because it needs that. So again, the one that is being used by Ray Filter goes inside the function here. If you have any others that you want to use inside of ah, the function, you have to use this, use keyword and then specify what those are. So that's what's going on there. Um, you know that may or may be not something that you've seen before, but a little bit more. Ah, oven involved technique there, So that's what we're doing there. Essentially, what we're gonna end up with is ah, an array files that has Onley files inside of it. Eso no directories will be listed. The other thing you have to keep in mind is all right now we have ipsum warm and warm. If someone you may say, Well, I don't need to do that because I'm not ever gonna have any directories inside there. There are some default directories dot and double dot that are sort of automatically included, and Skander will find. And so you need to filter those out. So you need to do this regardless. This part right here, you need to do it regardless of whether or not you plan on putting any directories subdirectories inside of here or not. So just keep that in mind. So if it's if the if we searched the directory and we filter out all the the directories inside of it and get just 1000 it's empty. Well, then we don't have any post inside there, so we're gonna return. False get post is gonna return False. Otherwise we're gonna do is we're going to actually get those posts. We're gonna create a posts array, and we're gonna leave that empty to start, and then we're gonna loop through each one of the file. So for each and then files files as file. So we're gonna loop through each one, and then we're going to build a posts array that is filled with objects that we get from get post. Now if we go up here or down here, two posts. Ah, get post. What it takes is the slug. Now, when we were building this Ah, what we get up here from files is these slugs. We get these names right here. So essentially, that's what we have is we have those, uh, we have those slugs, so we're gonna use get post to to actually go in and get those posts. And so this is actually going to go in like this, does here and remember, actually gets the file it parts of the filing to do to two different parts creates our object for So this is going to create our post object that we need. So we're gonna ultimately end up with a new array of objects of all the post that we've created that are in that Post folder. Now, we're gonna do something here called a ray sort by column, and we're going to pass in the posts and then date and then we're gonna return post. Now. This array sort by column is something we haven't created yet. And this is going to be inside of a utility file that that we have. And so we haven't created that. So we're gonna come over here to live from over here to live, and we're going to do new file and utility dot PHP hopefully exposed that, right? I don't think I did. Do you till the tea? I think I got that spelled. Right now we again need to go back to our index here, and we need to include that and actually U two lady. Okay, so will include utility. So that gets it all included. And then inside utility, we're going to create this function. Now, this is a function that I I looked up that, uh, I got some help from old stack overflow flow on. But essentially, what this does is you pass in the array that you want to sort by you ah, past in which this is for sorting in a ah multidimensional array. And so and we want to sort it by a particular value within the object. So let's go ahead and save all of these on and let's just take a see where we're at and widgets. So let's let's go ahead and do print are on posts. Okay, Hopefully that will give us something we can look at here and we haven't actually included . That's why we have included this. So let's go to, um I think we were using page dot PHP. Yeah. So we've included the header. Let's go ahead and include the footer now so we can actually see what we got going on over here. So footed up. PHP. Let's go ahead and open that up and let's see what we're getting here. Post dir post up Ph PN lying five. Oh, missing an f. All right. Okay, so we have this over here that we have to deal with because we haven't created that widget yet, But if we view the page source here, you can see we get this in our posts. When we crown our post, we get an object, we get an array. So 01 we get of objects. Have title slug. Author date. So what we want to do is we want to sort by this date. And so this is a multi dimensional. It's really ah array of objects. It's not really technically multidimensional array. But we want to sort by this day item here. And so that takes a little bit more than just running something like you sword or something along those lines. And so that's what this this ah function does here is it goes ahead, and it sorts that. So Ah, again, you know, this is one of things that I sort of got help with. Ah, did some research on this Found some different functions that that did the trick. But this is the one that I like best because you can pass in. You can also you can pass in not only the name of the field that you want to sort by, but also which direction and and so forth. Okay, so that gives us our array sort by call him here. We're sorting by dates. We specified the date, and then we returned our post here to get posts, and then in Ah, are widgets over here. We just printed that out and that went ahead. I'm printed that out over here. So that's what's doing that. So now what? We need to do is we need to come here and we actually need to build out the rest of this latest posts. Ah, function here. All right, So what we're gonna dio is pretty similar to what we did with categories. So first off, we're going to check and see whether posts is empty. So if it's empty and actually this one needs to be turned to empty a swell. So if it's empty, then we're just gonna return. False. And then, if not, we're going to build our widget again. And this is where things sort of get a little bit redundant because we're going to build a widget in the exact same way because we want them. We need a standard for our widgets. Weaken style them appropriately. So you'll notice we have div class widgets with widgets. Widget latest posts. I actually should change this and add widget and then widget down here so we can target. And, uh, with our CSS, we can target all of our widgets. Ah, at once. So widget widget Latest posts, widget categories. We can see we're doing spring F on the title were dropping in the title in between two h Ah , four tags were doing an order list list on Styled. It's the same HTML is exactly the same. The only difference is is that we are looping through posts instead of categories. Ah, and so on. And here we are because we're supporting number of posts than we are using that inside of, we are adding that so we're gonna set our count equal to one. And as we looped through each post, if the count is less than your equal to the number of posts rented, we're gonna add that item and count up. So every time this iterating, this will start at 12345 and then it will stop. So we'll get five items, um, in it. So that's what that does here again. Sprint half for each item or looping through each post were looping through that array of objects we just looked at. And for each object, Then we're just going to create a list item this HTML could change based off of what you're building here, and then we're using get Perma link and get the title. And these are two things that we I don't think we have Ah actually built yet. Okay, now, here is where you'll see this Because we see here, we're passing in. Post and post were passing in an object here. We're not using the one in the global space. Now, that's important. Because if I used get Perma Link or get the title here and I didn't pass in an object, it would grab the title from the page that we were on. Well, when you think about a sidebar, if you're on the about page, this would grab the title about for each one of these, as opposed to the actual item from this object that we got in sort of get post. So that is why we did that object equals Knoll when we created that other function so that we can support ah, this sort of thing. So the first thing that we're going to do to finish this off is we're gonna head back over to functions which I don't think we have open, so go to functions and we're going to create our get the title, and we're going to create our get Perma link so we'll just go ahead and come down here and actually, we do have get the title so we don't have to worry about get the title. But we don't have get Perma link. So we'll come down here, we'll create a new function called get Perma link and again, object equals equals no. And then we check right away. If object equals no, then we are going to grab from the global page variable. This is what specified through the past to us from Rourke. And we're gonna set slug equal to that page slug. Otherwise, if it's not know so else, then we're gonna set it equal. That means an object was passed in. So we're gonna set the equal to whatever the slug is of the object that was passed in. So in this case, over here Ah, in widgets again, we're passing in the object. That was the object that we got from get posts. That is this object right here. If we look at the source, the title is right here. So because we passed on the object, that function is going to use this title, not the title about from the actual page here. Okay, so that's again. I just want toe sort of make sure that's clear. That That's why we do that. Okay, so that's widget. Latest posts and then the last widget that we need to create A is just a simple text widget , and it is pretty straightforward. So let's go ahead. Come down here and we'll do function, widget, text and do title the default. I'm going to do a text widget, and then we're also going to do the HTML that actual text and it's going to, ah, support HTML. So each MT equals text goes here, and then each Jamal supported. Now you don't have to support HTML. I mean, it is a text widget. So technically, we could say we don't support HTML, but we're gonna go ahead and ah, do that here again. I'll just sort of dropped this in because this is just really pretty straightforward stuff . So it's mainly just building the HTML. It's the same thing. Class equals widget, Dutch, Texel, the type of widget. I'm gonna add widget again here. We're putting our title into an H four tag. We're adding in our html and then we're closing our def tag and where echoing the widget now we are just sending the html straight through. We're not escaping it. We're not doing any of that. We can do that in this context because in order to add this widget to anywhere in this, you as the developer actually have to come in right here like this and pass in all of that data. So there's no user inputted data in this sense, whereas if you compare that to something like WordPress and it's widgets, you are actually the admin a user in putting that data, you're not the developer in putting that information. So that's what's different about here and why we're not escaping that that html here, if you're gonna open this up some how to use their input input a data than you would certainly want to do Proper escaping Ah, and so forth. OK, so that's our text widget. So hopefully let's come over here and see what we have. And boom. Now you can see we have our foot and we have a list of our latest posts. We have a list of our categories. We have our about our text widget that we just created. Ah, as well. So we're starting to get the semblance of ah theme here in a template that that that's coming together. So of course, we still need to do Ah, the sidebar. And then we'll have to do our individual actual template pages. You are page templates are post template or category and and that sort of thing. And of course, that's what we're gonna continue to do as we go through the course. 9. Build the sidebar.php Template Part: right Now that we've got our widgets created, we can go ahead and create our sidebar as well. So we'll head over to our template files over here and inside. A default will go ahead and create a file called sidebar dot PHP and we'll go ahead and open that up and in here will create. We're gonna use the aside that's available inside of boots or early HTML five will do. Side class equals and we'll do. We're gonna dio call medium Force or into a four column sidebar here. And then we'll give this also the class of blawg dash sidebar like this and again. This, like I've mentioned throughout this, is dictated by Bootstrap Pond. Sort of the theme that I'm using here, inside here. We're going to give each widget Ah, class of P Dash three. And then here we can just drop in our widgets that we just created. So widget underscore categories. We can just call this function. That's one of the widgets that we created. And that's all we really need to dio And then for each widget that you want to add, you essentially just come over here and you drop it in like this and here we can do widget and we'll do latest posts like this. And that's that's really pretty it pretty much it accepted. We got to go over it and Page. And now we have to include our sidebar. Now, this is gonna look a little tricky just because we haven't built out the full HTML here. But at least we can take a look at the widgets and be able to see make sure they're showing up on DSO fourth. So I head to our about Page will refresh that, and you can see our widgets. Or over here we have a list of our different categories with the links. We have a list of our latest post with the links as well, so that's creating the sidebar. What we need to do at this point is we need to go into we need to actually fully bill out, build out. Now are are different templates for Paige and posts and all that sort of thing. Our our home page. In our category, you're gonna be a little bit different. But page and Post are really pretty similar in this particular instance. Of course, you can build them however you want, but that's building out. The sidebar is pretty straightforward and pretty simple 10. Build the Page Template: all right. Now that we've got a lot of these pieces in place, we can go ahead, fill out and do this full sort of template here. So what we're gonna do is we're gonna go into page dot PHP first, and we're just gonna build this out. So we have our header so that that's all. There we have our footer, which again, is gonna be the same for every page here. And then we have our sidebar, which will be on page dot PHP. It'll be on the post template. It won't be on the category and it won't be on the home page, so it's not on every page, but it is something we re use quite a bit, and we just sort of need to fit it in. And you remember this was four columns wide, and we, you know, in a bootstrap set up, we have 12 columns. So arm are main content area that we're gonna create for our about page needs to be a columns. So that's something to remember as we go through this. So what we're going to do here is just jumping to some html. And now this is again this is sort of just standard. This is coming primarily from Bootstrap. We're gonna create a a main tag here. We're going to give a class of container, and that's dictated by bootstrap. And this is going to you know, this our sidebar end our content area. Our goal going to be included inside of that, um, we'll talk about that in a second. But let's just go ahead and put this up here. We're gonna have some other stuff in here, but let's go ahead and put it up here so we don't forget right inside of this. Then we're going to do a row again dictated by bootstrap. So we're gonna have a new row. And again our sidebar is going to be inside of that row. So the game for familiar with bootstrap, This is pretty standard stuff. We have a container, we have a row, and then inside of that row, we're gonna have an eight column Ah, an eight column with column and a four calm with calm, our main area and our sidebar. So that's what we have here. So, you know, pretty straightforward stuff. So now we, of course, need to create our main calm. So we're gonna give this ah, class equals Call MD eight again dictated by bootstrap. And we'll give it blawg main like this. And that sort of dictated by the theme because the theme, ah, uses that some of its CSS. And so that's the main sort of container. And so again, just toe sort of reiterate this. We have a container, we have a row. We have eight columns and then member sidebar was four columns. So you're pretty. Probably If you worked a bootstrap, probably pretty familiar with that inside of our container. Then we're going to do another div. And we're going to give this class of Blawg Post and that sorted to separate it from Ah, if you we wanted to do anything else. For example, if we wanted to add, like an author block, you would put that outside of the Plawgo post because this this device really to designate this is the post itself, and then you would maybe have an author box or you have a comment area, etcetera, etcetera. So this is just saying this is going to be where the actual post is going to reside. Inside of that we're gonna have our title tags will do in h two will do class equals And this is blawg Post title again dictated sort of by the theme that we're using from, ah, bootstrap here. And then we're going to use, ah, the title, which is, ah, function. That's going to drop in the title here. Now if we go to let's go ahead and open up template dot PHP. And so we have title tag over in post. We have get posts and get posts. So I don't think we've done title yet, so that's something that we will probably have to right here. But that's essentially just gonna drop in the title a post and then we have now this is optional, but I went ahead of included it in here. So we're going to an if statement if and we're going to check a function called has Image, which we haven't written yet. So if it has an image meaning if this page has an image associated with it and we'll go ahead and do and if on that, so we don't forget, So if it has an image, then we are going to do we're going to actually create the image. And so we're going to use a function called the Image, which we haven't created yet as well. So if we go back to our about page, we're going to get several heirs here for call toe undefined function, the title and so forth. So what we need to do is we need to go through, and we need to actually then create thes. So we're going to go into our ah functions dot PHP file here. And you see, we have get the title already here. Right? So all we need to do is go into template and we need to create a function called function. Yep, the title. And we're just essentially going to return what we get from get the title. Now, of course, we're gonna do object equals No, just like we have with these other ones here. And so if we want to pass in an object and get the title, we can do that. And then we're gonna do echo, get the title like this and will pass in our object if it exists like like that. So that should take care of our title for us. So let's go ahead and come back over here and you can see we now have our title, which is about here. Now we're getting 14 has image here, and so we don't have ah in our library. We don't have one called. We don't have ah file called image dot PHP yet. So we're gonna create this one. We call this image image dot PHP. And then, of course, we're gonna go to our index, and we're going to include that in as always. All right, so there's image, and then we're gonna go ahead and open image and inside of image we needed to create. First we need to create has image, so need creative function that will check to see if a particular post has an image. And if it does, then we're gonna grab it so we'll do Function has image and will do again. Object equals no. And we will check to see if the object is indeed no. And if it is, then we're going to grab the post object from the global space, as I've talked about before, so we'll globalize our page object here, and then we're just going to do in a check for empty. So if it's not empty if the ah page image property. So if there's an image associated with this Ah, this page, then we're going to return. True otherwise. Ah, then actually, we don't. So if that we could just run our else off over here and I'll show you why here in second? So then we're gonna This is so this else is not running off of this. If right here, it's running off of Ah, the object equals knoll. If Okay, so if it's ah if object equals object does not equal. No, the meaning and object has been passed in. Then we're going to just check the object that was passed in two C. We're gonna check basically the same thing. So are still object image. So if the the object that was passed in has a nim ege associate it with, we're gonna return true. And then if none of those returned true, then we're just gonna return false down here. And so the reason I do it that way is because instead of running in this, if block right here running and else and then saying return falls and then doing the same thing down here. I can just Onley return true. If there's actually an image and any of these and if there isn't, both of these will fail and will eventually get down here to return false. So it's just a little bit more concise. So that is our has image check. So if we come over here now, that is Ah, that's okay. We're not getting in an error on there. If we go back to p page dot PHP, then we have this this function called the image. But before we can do that image, we have to do one called get the image. So we'll come back over here, we will do function, get the image. And essentially, what this is going to do is just get the image from our again whatever either object or our page variable. So and will do again. Our check is so if object, there's no object that's passed in. Then we're going to globalize our page, and this is going to get fairly redundant. We do this pretty much every time, and then we're going to simply return page image again. Some of this may seem like, well, you're not really doing much here, but it saves you from having to write all of this code over here. So it just helps keep Kate keep things more organized and cleaner. And then that's reusable so you can use it again and again. So that's the whole point of doing this. So otherwise, if the object is not also an object was passed in, then you can guess. We're just going to return object image like this. And then now if we get down here, we could just return false, Um, if we wanted to at that point. So if there's an image in either the page or the object, it's it's going to return that image and this is going to be, ah, the u R l of the image. That's, um, that's important toe toe. Understand Here it's not the actual whole image tag or anything like that. So that is, get the image, and then if we come down here, we can do function. We do the image, and this is where we're actually going to build our HTML. So there's a couple things here. I'm gonna go ahead and write this out and then we'll talk about this. So I'm gonna set size equal to full like this by default, and then object equal to know, as we've been doing now, we re first need to talk about this. Size equals full thing. We actually need to create some support for this. So one of things that we need to create is the way that I've handled it here is I'm using an options file. So we sort of close all this up again so we can kind of see what we're dealing with here. Close all these up. So right in our route here in Rourke, we're going to create a new file. We're gonna call this options that PHP okay. And we can go ahead and open this. And what this file is really meant for is it's things that aren't necessarily in our conflict. Violet were configuring. Ah, you know, this this, I guess, could be considered configuring in that sense. But I just sort of went with it, being something that is more of setting certain options, like weaken, we don't necessarily have tohave. These options is sort of the point. The con fixed of stuff. We absolutely have to have this stuff we don't necessarily have to have. And so I'm I'm using it as an options file. You could have course, choose to do it a different way, But this is what I've done. So I'm gonna create ah, in options I'm gonna create. One of the options I'm going toe have is something called image sizes. And this is going to be an array, and we're just going to specify some different image sizes. This is sort of important because, ah, depending when you're displaying images in different places throughout your site, you want to make sure that no, you're displaying a size, you're displaying the proper size so it doesn't get distorted and so forth. Now we're not going into because of the way we're doing this. We're not, You know, we're not Ah, uploading and then sort of cropping and doing all that. We're not getting into any of that here, but this will allow us to sort of, ah, display different sizes. So one of the sizes, as you probably guessed, is one called full and we're just going to specify what that is. And so we're gonna create a a sub array Here are second level array and in that for the full, we're gonna is gonna set the wit, which is going to be equal to 730. And this is again sort of dictated by what we've got from bootstrap. I just sort of looked at What? Okay, what's the size of the image when were displayed at a wide wit, and so this is sort of the biggest we're gonna get here. Okay, so we're going to set the width and the height, and that's it. So we're just creating an image size here, setting width and height for it so we can get what we want when we want to display this. So then we're gonna do thumb, and that's again going to be an array. And in that array will have with of 300 and ah, height of 1 69 All right, so those are two image sizes and that will again allow us to reference this in our image function that we're creating course. We need to come over here now. This one I actually put right below con fig here just because these aren't necessarily these air mawr configuration and set up tight files so I keep those sort of at the top. You could comment this properly and specify that, but, um, so I sort of keep those two of the top. It's really just sort of my preference. All right, now that we have that, now, we have some context for the size here. So by default, the image is going to use the full size. Um, so that's that's important, Understand? Now we're going to do global. We're gonna globalize image sizes. Could remember we created them in that options file and we just dropped him in there. They're not inside of a class or anything like that. So thes image sizes this variable exists in the global space, so we need to globalize it first so that we can then use those Now, what we're gonna do is we're going to check to see if the size that was passed in is in our is in our options file. If it's one of the sizes that we support, so we're going to do if and we're going to do not in a race. So if it's not in the array, and what we're gonna check for is the size that was passed in. And the ray, we're going to check his way into a ray keys, so it's gonna give us just the keys, and we're gonna do that from image Sizes. So what that's going to do is is going to take this array and it's going to create a new array with just full and thumb so that we can run in array on it. And so that's going to check to see if the size of was passed in exists as one of the image sizes we've specified in our options file. If it does not, we're just going to set size, equal toe full. We're gonna set it back to whatever the fault, the default waas. Okay, so I think that's pretty straightforward. Now that we've got our size set, then we need to get our image. We're gonna do image you are. L equals and will call, get the image like we ah, the function we just created and will pass in object. Ah, so that get the image can determine if it's knoll or not, etcetera. So that's going to give us our image oil, and now we can actually build our image tax we're gonna do image, and we're going to that equal to Sprint. We're going to use Sprint F. And our format is going to be image source equals and we'll put in our first place holder. And then we're going to class equals and we're going to Rourke Image Dash, and we'll do our second place holder. And then we're gonna do Ault equals and our third place hold her. And then we're gonna do our width. And that is equal to our fourth place holder. And then height is equal to our fifth place holder like this. And that's going to create our image tag. Now we need to populate are place holders, so we'll come down here. Go ahead. Set this down here. You can clean this up a little bit. All right, So our first place holder is the the image source, so that's got to be our image. Oil. So image underscore URL our second place holder, then is our size or doing Rourke dash full. So that will allow us to target that image size in our CSS. So we're just gonna do size like this, and then our next placeholder is are all tags we're going to set that to the title of this particular page or post or whatever we're dealing with here. Ah, and we're gonna pass an object again because we have the page object case. We need that. And then we're going to set our with and our height. So we're going to do image size is like this, and then we're gonna pass in the key. Remember, we have the key stored in size, so that's going to go in because we're specifying this if we have full set is gonna come in and grab just this array. If we had Thumb said, it's gonna come in and grab just that array. So that's going to give a size. And then what we want from that array, because it's a multidimensional Ray is we want the width like this, and then same deal with our height. We're just going to get our height instead. So we're gonna do again, sighs, and then we'll do height like that. Okay. And so ah, this is gonna drop in all of these values into our sprint f here and give us an image tag, and then we're just going to echo image like this Now, on our about page, Uh, if we actually come up here, we don't have an image specified. So this actually right here it says him if has image, it says no, it doesn't have an image. And so it doesn't display the image here. But if we want to check this real quick, we can come up here to content and let me just open Ah, one of these post Roe quick and see what we have in here for Image right swing. Come here and let's go now to our about page, Let's open up about. Let's just specify that we have an image like this, okay? And let's see if our code works or if we've got some areas that we need to deal with, right? So it's is trying to get property of non object image dot PHP online 24. So let's see what we're dealing with on line 24. Alright, Return, object image. And so what happened here is instead of checking, deceive is equal to No, no, no. We were setting in equal to know. So that was probably where that air comes. We didn't do that up here, so I think we're good. Let's let's go back up here. And there's our image right there. So that that gives us sort of all of our image stuff. I'm gonna go back into about and just get rid of this just so we can sort of always tell the difference. Here, boy, I'm really messing this up here, all right? And if we go back to about our image should disappear. And, yeah, we don't have any heirs. OK, so that's our image stuff. We now have that set up to where we can use all of that image functionality in our ah, in our template. We're not using it about in the page template, but we will used in Ah, the post template. All right, so now we've got our images set up. Then the last thing that we need to do is we need to do our content, and so will do. PHP. The content is the tag that we want to use like this, and ah, I honestly I'm not sure if we written on yet, and no, we haven't written that one yet. Okay, so we need to write. Are the content um Ah, function here So we're gonna go into template again. And we did the title. Well, you can guess what this is gonna be going to function. The content object equals No. And we're simply going to echo get the content. Okay, now, we haven't written that function either, so we're gonna have toe. Ah, right, that one as well. So we are going to go into the functions file here. We need to write, get the content. Okay, so we'll put that here between get the title and get Perma link. So function get the content, and again we're going to pass in our object, and then we're gonna check and see if object is no. Like I said, this is going to get a little redundant or doing this on all of these will do it for pretty much All of them. Well, globalize our page variable and will return page content. So this is sort of what I was saying before. How once you get it to where you have, you have the data being pulled in that you can access and you have You know what template you need to load the coat. The rest of the code from there. It's really pretty straightforward. All we're doing is grabbing that if we need to were grabbing that data from that global space from that globalized page object here and we're just grabbing what we need for a content title, you know, etcetera, etcetera. So, no, it's all really pretty straightforward here and so again else if it if there's an object that gets passed on, we don't need the data from the global space we use the one from the object that was passed in that will make more sense when we get to the category place as well. I think we did it with our widgets. Ah, a little while back. All right, so get the content is written, the content is written. So let's check. We should have content. You can see there's our content. So now we have a full about page here that has our title has our content. We have a sidebar we have ahead or photo like we have to get to the point where we have a real template. Of course, that's just our page template, right? We also want toe, make sure and do our post template as well 11. Build the Post Template: Now, the thing about the post template is it's really, really similar to this. So will come here and we'll go to post and we'll just drop in what we just wrote in page. The only thing that's really different is we're just gonna add a little metadata. So if we come down here under the title, we're gonna create a new paragraph, we're gonna set this eq the class equal to Blawg post meta, and then we're going to do the date, and then we're going to Dubai and PHP the author. Right? So these are again two more functions Ah, that we need to deal with here or that we need to to right here. Um, I believe that these are in the post the post ah, template for the post functions file over here. So we'll go ahead and let's go ahead and close up content just so I can see we're working here. We go into lib, and I think we're in. We're gonna be doing these inside of post, but actually, we're gonna do them inside of template. Okay, so we go into template and we need the date and we need Ah, the author So this is getting pretty straightforward. So I'm just gonna go ahead and copy this at this point, cause these air all pretty much the same. So we'll do will change this from the title to the author and same thing with down here. Just change that toe author. And that's our Get the author function for Come down here. You can dio date and date. Okay, So the ones that do all the work are really the get functions. Okay, so we're gonna write that inside of our functions file here, so even these are really sort of pretty straightforward. We've We've kind of done this a couple times. So all again, speed this up a little bit. But all we're doing is grabbing data from either the object that was passed or from the global page variable. So we're gonna do change this to get the date or actually will do is to get the author first, get the author we're gonna check if we're gonna check this just like anything else, and we're just gonna return page author of that set or object author if that set. So, at this point, we're sort of just copying and pasting code here a little bit. And now again, You you You will look at this and say, Well, you know, you're you're not reusing code, right? You're rewriting the same code over and over again, and that's true. But we're doing so as a convenience for writing the templates. It's one of the things that's a little tricky about a course like this is I'm writing the back and framework at the same time. I'm running writing one of the front and templates. So this is the back. What we're doing right now is the backend framework. And the reason we're sort of reusing this code is because, ah, we are creating convenience functions now. It is true. I could probably go through here and I could, um no, Instead of writing each one of these, get the title, get the author, get the date, get the content, get Perma link, etcetera. I could probably turn this into one function that something like get post data and you specify object, and then title or author or content, etcetera probably. Ah, definitely could do that. Um, just haven't You haven't done that at this point. And again, as I mentioned big meaning. I sort of want to be really verbose to not sometimes when you do these sorts of things, you get to concise. It gets difficult for four people to understand. But just know that you could do that if you wanted to do it. But again here, we're just gonna go with date and date. Okay, So that's going to get the author and the date for us. If we go back to post op PHP, then we have those added and that's mean, Ah, we are gonna add one more thing which is down here. Then we are going to do, Ah, the post categories. And we're actually going to get the categories that this post is in. Okay, and this is going to be inside of our category dot PHP file, which we have created. Okay, we've credit created that one over here, but we haven't done getting the post categories. Okay, so we need to go ahead and write a couple functions to get the post categories. So let's go ahead and come over here to category and let's just come down here and let's start with getting the post category. So we create a new function called Get post categories and we will do post slug equals No. OK, and we'll do globalize page. And if post slug, this is sort of what we were doing before. I think I just wrote this in a little bit different way. We're just checking if an object is past, we're gonna use that slug. Otherwise, we're gonna grab it from the global space here. Okay, So if a slug is passed in, have post slug equals, that doesn't make any sense. Post lug equals no. Then we're gonna do post slug equals. Ah, and we're going to do page slugs we're gonna grab from our global space. So, essentially, if a post slug is not passed in, we're going to try and grab it from the global page variable, which should have it. Okay, if it is passed in, we're going to use whatever was passed in. All right, now what we're gonna do as we're gonna get our categories, we're gonna get our main category lists we're gonna do get and we're going to categories. So that gets our full category list. If that's empty, then we really don't need to do anything else. Will just run a quick check on if that's empty or not. And if it is just going to return an empty array, all right, and then we're going to do post categories. So we're creating a new array called Post Categories and we're gonna run a loop, and we're gonna populate this array is what we're doing here. All right, so we're just doing that so I don't get any notices on and then we're going to do for each and we're gonna loop through our entire categories list, so category And they were to do as category slug category data like this. So and then what we need to do is we need to check and we need to see if ah, essentially the the post slug If you remember the way that our categories or build if we come to content and we come down here to categories, remember, we put the lists of post by slug in this designation here. The reason we had to do that is for what we're doing now. Ah, so that we can see if a particular post is in a particular category. So that's why we need the post slug so Now we can We have our fullest two categories and each one has the post that it's in. We need to let loop through that list and see if the post slug that was passed in what categories it belongs to, if any. So we're gonna do if I'm going to do in array and what we're looking for is the post slug will receive. The post slug is in category data, which will be the actual category object or a ray here, and we're going to see if it's in the posts. So we're checking if our page slug is in this right here. Okay, so that's what we're That's what we're checking here. We're going to see if it's in there. If it is, then we want to. We want to create a list of all of those. So we're remember we created a new a new array called Post Categories. We're going to populate that swing set post categories, okay. And we're gonna do the brackets so that it gets added and we're going to do categories we're going to do category slug. Okay, So, essentially, what that's going to do is we have our entire categories list. If the Post look that was passed. If that slug is in one of the categories in our list is going to take that entire it's going to take this entire ah, ultimately element here and sort of a multidimensional when this gets turned on PHP is gonna be multidimensional floor. But it's gonna take this whole thing is gonna put it into our post categories array. So So, for example, here, Laura MIPs, um, that's gonna be in both demo and test. So when we're done here with with this right here, we're gonna have both of those categories in Ah, an array here, and we're going to return post categories. Okay. And let me just do real quick down here. Since this is what we put into our template file, we're going to function. And I believe it was the post categories that may just double check and the post categories . So we do the post categories and let's ah do post categories equal and will do get post categories like this and we're not going to pass in. We're gonna just we're not gonna pass in a post slug. We're gonna grab the one from the page variable. And then let's just quickly do so You can see this print are post categories like that. Okay, so, uh, and actually we haven't done all right. We did do our post op PHP template, so we should be able to Let's go ahead and head on over it. A warm if some see what we've got here. All right, so we've got our title. We've got our metadata. We've got our content here. And you see, we have an array. Looks like we may be. Have something going on with our footer. Will check that in a second. But actually, let me go to view page source so we can see this little easier. But if we look down here, we printed out the writ that the post categories you can see it's just a array of the categories that that this particular posts belongs to. Ah, if we go to Ipsum Laura, um, and we view page source, it only belongs to the one category. So that's what's in the area. So that's what all of this code over here does. So now we essentially we have, um we have the list of categories that this Post belongs to. Also, all we really need to do then is to print out a list of those particular categories. So that's what we're going to do here. So what we'll do is we'll go ahead and close PHP. I'm gonna embed a smell rank in here, and we're going to do div class equals post dash categories like this. And then we're going to do I'm gonna do this a little bit different this somewhat driven by bootstrapping the template. But we'll do SPAN class equals post category list header. And we'll do categories like this and we'll close our span and then down here, come down. We're gonna loop through and create our list so we'll do PHP. I will do if empty post categories and we'll do echo none. So if there's no categories is just gonna echo none, then we'll dio else and we'll do a ray, um, array. Keyes equals array keys, post categories like this. We're gonna get a list of the keys. So all of the keys that are in our past categories, that's gonna be the basically the slug of the category, and then we're going to check for the last key. So we're going to last key. You'll see why here in just second equals end and we'll do a ray keys. So essentially, what Lasky is gonna give us isn't tell us what the last key in this ah array is. And we're just using this mainly for cosmetic reasons. Now that we have that we can do for each, we do post categories, we're gonna loop through our post categories. We're going to key and then category. I think this and we're going to The reason we needed that last key is just to get the comma right, So we don't put it extra comment The end of a cat, the last category when it doesn't have any coming after it. Like I said, it's purely a cosmetic thing. But we'll do, comma equals and we'll use our turn. Eri eso will come inside here and we'll see if key if the current key is the same as what we figured out was the last key. Okay, that's all that's really all we're doing. If that's true, then we're gonna just return. We're gonna echo out basically or print out nothing. If it's false. Then we're gonna put down a comma. So essentially, what this is going to do is ah, if it's not the last key we're gonna we're gonna have a comma. If it is the last key, we're not gonna have a comma. And then from there we can do print f and our format is going to be spanned. Class equals will do post category list item and we'll do a link A H ref equals and we'll do our first place holder, Which is of course, is gonna be the you Earl Ah, to the category and then close that up and we'll do our second place Older, which is gonna be the name of the category. Of course, clothes are h tag will close our spanned tag like this and we'll do our percentage s here, which is gonna be far comma. OK, so that's our formats. Basically, just span tag with ah ah link inside of it and then we need ah, it's going close that up so we don't forget and then we need our to fill in our placeholders. So the first thing that we need is we need our category link so I can get category link and We've already done this for our widget. And what this needs is the slugs we're gonna do category reference our category object. We're gonna pass in the slug, okay? And then we need the name for a second place. Soldiers were going to category, and we're gonna do name, and then we need to know for 1/3 place over. If we need a common thoughts, we're gonna pass in comma like that. Andi, that's going to give us that is going to give us an air because we didn't close this off here. All right, so the last thing we need to do then is we have that. We just need to close up our and if here and you might be wondering, a Is there a reason why you're using, you know, the Coghlan's and and stuff instead of, ah, the curly brackets? Not really, I think. Probably when I first wrote this, um, I split all of this out and I was using the colon notation, sort of, I guess maybe not somewhere, like, sort of like in our like we're doing here. I probably wrote it like this and then later decided to change it and so I just sort of left. Allow it is theirs. You could very well just ah, change this to back to curly brackets and so forth. I need to end, and for each year as well as that will give us a new air. All right, So what that should ultimately give us Now, if everything's ok is a list of the categories and let's see, called the Undefined Variable last. So let's take a look. Uh oh, I think I do less I put in last Ah kee the last. So let's do last key here to correct that. And again, some is going on with the foot A hero. Oh, look into that and see what that is. But if we come here, what we ultimately get ah, looking for That's the footer right here is we have a list of ah, that's the post category list. Right here we have a list of the different categories and ah, if I can click this right here, you see, I click this This will take me to the cask category. Now, we haven't created the category page yet. Um, but we will do that. So this is giving us a a list of Let me just hide this so we can see this real quick. So let me go to footer, Blawg, Footer! We'll just do a display. Nano quick. Right, So you see, here we have a list of our categories here. If we go to Laura MIPs, Um, and we have the same thing. So we have a list of our categories in this case, it has multiple ones, so it's gonna list all of the ones that it has. Okay, so it has demo test. You knows we don't have the common at the end there. So that's what all that code does gets us to the point where we can sort of display all of these different pages and ah, so forth. And now we have our page template. We have our post template, and we can now start to look at our category page, and we could start to look at our home page and then cleaning up some of this CSS and so forth. So we're getting really, really close to having this thing finished up. And of course, in the rest of lessons, that's what we're going to dio 12. Build the Category Template: all right. Now we can go ahead and dive into our category template here. So if we come over here, we'll see this when we were at supposed to me cooking category, we'll see. Right now we have in this text in here that says category like this, we're gonna work on this template and will come over here to it, and it's gonna be similar to the others in that we are going to do includes for a header dot PHP, Um, and we're going to do an include for footer dot PHP like this. So everything will be inside of that pretty much. All your page templates will be this way. Now, this one's a little interesting. And this is where we're going to see all that sort of monotonous or re occurring stuff or we're using object equals and old and all of that. This is where that really comes into Play is in here because what you're ultimately going to have is you're gonna have information about the category itself, which is going to be coming from that global page variable. But then we're also going to display list of posts in that category, and we're gonna be. We're going to be out putting data about each individual post the way that we've done this . We can use the same functions for both. And so I'm going to sort of show you. Ah, that here. Now, I'm gonna go ahead and create and just sort of, uh, drop this in here because this is kind of long and convoluted, but this is the div that we're gonna wrap everything inside of now. This is driven by bootstrap four. And just to be 100% honest, I don't understand quite exactly all of this yet. I'm still sort of diving into a bootstrap for his p x y i p y all that stuff. I don't fully understand it all yet because I haven't ah, spent the time to go in and read and figure out what all of it is. But I know that for the template that I wanted to use this was for the way that it looked. This is what I needed to do. So ah, again, Uh uh, with the HTML in the bootstrapping, so all of that can change depending on how you want it to look. All right, so once we're inside of that, then the first thing that we want to do Israel and created a Chuan talking we're gonna display information about the categories we're gonna do class equals and will do display four as four. And inside of that, they were going to do category colon and then we're going to do PHP the title. Remember Titles? Gonna grab if we don't pass an object into it's gonna grab it from the global space. Well, what is in the global space is the information about the category and then where we can do well what we've done before, we're going to do PHP if has image and again, we're using the global category. Space here will do PHP. And if and then we're gonna just display the image. So PHP the image and we'll do the full image. Okay, so if it has an image, we're going to display the image. So let's just sort of look at that at this point and see what we have so far. So you can see we have category. We have demo must not have an image, So there's no image being displayed here. And now we can add in the last bit. That's going to be specific to the category itself and will do. Ah, Div class equals lead and we'll do PHP the content. And again we'll just take a quick look at that that's going to give us the category description that we put inside of our our file for this particular category. So again, notice we're using the functions, the title, the content, the image has image just like we did before. And it's displaying category information. Now that's going to sort of become important as we go through the rest of this. Okay, so now what we need want to do is we want toe in a category listing. We want to display a list of the post that are in this particular category. So what we need to do is we need to get ah, we're gonna get a We need the those posts case we're going to do category underscore posts is equal to get category posts. Well, we don't have that function yet, so we're gonna go ahead and create its will. Come over here to this. We're putting all of our category function and write functions, and under here will create a new function call to get category posts. And so what we'll do is we'll use the global space. So Global page, like this global page okay, and will set post slugs equal to page and then posts. And okay, so if you remember from over here, actually the other way around So if we go to it's, um I think we Yes. So we have posts here, but when we convert it, we we converted it to post underscore in is what we're double underscore in. So that's why we're using that. Ah, that here as we come are here cause we're over here because that's what we converted it to when we when we created, are made our content available deeper inside of ah Rourke. Okay. But that's what that's gonna do is isn't gonna give us a list of the slugs from here. OK, so it's going to give us this list of slugs. Here's what ultimately that that gives us so if empty post slug. So if it's empty, then there's no point in doing anything because we don't have anything to work with. So we're just gonna return false. And then, if not, we're going to create a new array called Posts, and we're gonna populate that array. And so we're gonna do for each we knew post slugs as a slug, and then we're going to to post your posts. So our post story we're gonna populate it with and we're gonna run get post and passing the slug so that Ah, um well, let's see, I'm not of Hunterson. If we should have done get post already. Yeah. So I think we've done get posts already. We would have got an air there if we had. So that's gonna get basically is gonna loop through each of the slugs that that is specified in our ah, and our file that says that this post belongs to these categories. So is gonna loop through those post slugs, and it's going to get each one of those posts. So each element in this array is going to be a post object that we can then work with. We're gonna again run our array sort by column that we talked about before going to run it on our post array, and we're going to a based off of the date because we want to sort these by date, and then we're going to That needs to come outside. I would not make sense in sight of the loop, so we'll sort it outside here, and then we'll return posts like that. So that's going to give us a list of the posts we come over here. We do print are on category posts. I think this should give us Ah should be able to see a list of the posts inside of this category. Okay, so you can see we have an array element inside of it. We have an object, and we have the title of the post. The slug, etcetera. If we were to go, it's go test. Let's go to test. Has two posted it? So we have the element at the zero spot, and we have the element at the one spot here as well. So there's two posts inside of this one. Okay, so we're getting our post data now. All we need to do is loop through and display. Ah, that data. So we have our category information. Now, we've got a list of our post for this category, and we just want a loop through and display some output here. So what we're gonna do is we're gonna create a new container. So we're gonna do div class equals container and category posts like this, and then inside that we're gonna have a row, and then we're gonna loop through our posts on we're gonna create our individual column. So for each, we're gonna do category posts as category posts like this. Let's ah, and our for each. Like that. Okay, so now we just need to create our output. So we're gonna create our column. It's gonna be a four column wit. So Div Class equals and we'll do call empty four. So each one of them is gonna be four column widths and then we'll do Active class equals blawg Post, and we're gonna do text left because of the way this particular theme works. It centers everything in this, uh and we don't I want it all to the left, and then we'll center what we want to center. So now we're inside of an individual block post willing to h two that we're going to do. Ah, actually, let's dio class class equals and we're going to do blawg post title. Just sort of like we did before. Give her that. It's to tag. And now in here we are going to create a the name of the post and a link to it So we'll do a a treff equals Will dio echo will do get Perma link. But now okay, this is where we this object stuff comes into play. We're gonna pass in category, underscore post like this. So that is coming from our for each loop. This is the post object. It is this post object right here or there that were passing in. So we're telling our function. Use this. Ah, use this object to get the Perma link from to create the Perma link, not the one in the global space. The one of the global space would be a link to this category. Already on this category page, we want a link to the Post. So we're passing in this post object. So that allows us, in one instance to use this function in a global sense and on the exact same page, be able to pass in an object and use it in a different sense. So that's the value of this. So then let's do another one here, which is close our tag. And then we'll do PHP the title. And again we'll do category and will do. Post will pass in our category post object like this. Okay, so let's go ahead and save that. Let's take a look over here to make sure. So now we're getting the name of each individual post, not the name of the category. So all that work we did that redundant work we did with object equals knoll now makes it so we can use the exact same function here and here, but get two different results. This grabs grabs it from the global space for the name of the category. This grabs it from the object that we passed in and gets the title from that because it is in there, it is in the object so very useful. It allows us to be able to build something like this. All right, so now we're just sort of building out the rest of it. So p class equals blogged post meta, and we're going to do sort of like we did before so we can do PHP. The date again passed in our object category post So I get the date from that and we'll do by Ah PHP the author and category post like that. So that is our metadata. Come down here. Now, we're going to see if this post has an image. So if has image again, we got a passing category post like that. So if it has an image, then we're going to display the image on Will do the image category post. I actually just want to check for a quick Let me go ahead, because, uh, just refresh this image or glib image Stop PHP. So undefined property image. Okay, so let's open back up our image file and the image. And so we passed in our object here. We passed it here. Oh, and actually, we need to We need to specify are size. That's what's going on here. So here, we're going to do the image, but we're gonna do Ah, we're gonna do the size is gonna be film like this. So So now we're getting our thumbnail, but we want to link these up. So we're gonna wrap this image tag inside of a leaks, were going to do a draft equals and we're gonna do PHP get Perma link category post that's gonna get us Are you are l And then we'll just close it up. All right, so if we take a look at that, we now have our images should be linked to Ah, Did I pass in? Ah, just category post, get Perma link. We need to do echo get Perma link on this. Air links weren't looking right. So All right, so now going. Oh. Mm. Link still are looking right. So let's go. Maybe something is wrong. Let's go toe post and it must be functions. Get Perma link. Okay, so in our get Perma link function here Ah, we did all the logic, but we just forgot to return the Perma link. So are the slugs. We're gonna turn slug like this and actually let me double check right here, because it's I think we actually need to go through and build out the permanently care. Yes. So I said just return in the slug. What we're gonna do is we're going to turn. Ah, Sprint f the problem over here. If you look the link is it'll just linking back to this page for all of these and I knew that wasn't right. So something wasn't right here. And it comes back to this get Perma link function here. So we're gonna do percentage. We're going to format samy percentage s slash Send us o placeholder slash plate placeholder and we're going to use home. You are l and we're gonna use slug. So what this get Perma link function is gonna do is it takes a slug or it takes an object is gonna get the slug. It's going to get it from the global space as going to turn the home. You, Earl, wherever this is installed, slash the slug which is how are you? Are are you RL's are set up. So if we come through, we hadn't noticed this before. Um, because we hadn't necessarily been using this this extensively. But now, if you look over here, if we click on this, it takes us to the Epsom Lauren page. If we click on this, it takes us a warm if some page same for images here. These air now all linked up properly. So it's an issue with our ah, get get Perma link function. Okay, so we have that that'll sort of set up. Um, we can go back to our category template page here and sort of keep going. So we now have our title. We have, ah, our image that we want to display now weaken do. Ah, an excerpt. And so we'll do PHP the excerpt, which is a function we haven't created yet, But we will create in just a second soul do category post like this. And so let's go ahead and let's take a look at our excerpt function here. So excerpt is going to be in the post dot PHP file. Here's will do exits appear Bill de Post. Ah, uh, maybe it's in template, I guess. Okay, so let's go ahead and check, Template. All right. So again, in this template part here, this is gonna be pretty self explanatory. So do function. The excerpt and object equals No, but we're gonna have a second parameter here. We're going to set the links equal to 2 80 like this, and I'll explain what that isn't Just a second, but we're then going to do Ah, we are going to do echo. Uh huh. Do echo get the excerpt. We're gonna pass in object and links that I'm going close this up. Now. I think my file this, you know, this is all white it out here and actually think this has to do with my files is going and just reopen this. Yeah. Now we're back. Good. Okay, So sometimes this cloud nine does. I don't know why, but all right, so you can see the except we're just calling. Get the excerpt. Were passing in the object, and we're passing in the length here on. Looks like I got a typo, and then we need to go to our functions file, and we need to create they get the excerpt function, which is going to actually do all of the work here and again. It's doing this thing where it's let me go and reopen this. All right, well, let's come up here and under content. We will do function, get the excerpt, and we're gonna object and links ankles to 80. All right, So what we're going to do is going to do we're gonna first get the content. You set that equal to get the content, and we're gonna pass in our object, and then we're going, so that'll get us the content and that that will do all the work of checking the object and so forth. And then our excerpt is going to be equal to, and we're gonna use the function called trim words we're gonna pass in the content and the length so trim words is another ah, function that we haven't created. And then once we are done with that will return the excerpt. So trim words is going to be in our utility file, I believe so, we'll go ahead and create this trim words function inside of it. Now, I'm gonna drop this in. I don't wanna This is one of the things I don't wanna take credit for here. So I'm just gonna drop this in and explain it and give you the link. So this is a code originally by chirp Internet here. Ah, I did some research on different ones out there, and I felt like this was the best, the one that I like the best for this particular scenario. This one doesn't actually go by words. It goes by characters, but it doesn't just chop something off. No, it'll go to the end of a sentence if it's within a certain number of of words and so forth and it'll or if it's not, then it'll do. The dot dot It's just unintelligent sort of way of doing it. And so this is actually going to be the thing that ah actually trims The X serpent gets us the excerpts. So we're gonna use trim words to get the excerpt, and we're gonna use that in our get the excerpt function and then over in our category. That's what we're going to display here. So we take a look at that, then we now have or ah, we now have our excerpt there. So the last thing that we need to do is we need to create our read more buttons. So if we come down here under the excerpt, we're going to do P class equals and we'll do read more and we'll do. Ah, any class equals and we're going to button This is all bootstrap, but in primary on actually a treff and we will do Let's be echo get Perma link category post like that, and then for a text registry, use read more like so. All right, so that should create our read more button for us, Lincoln. See, now we have a read more button we click. It takes us to that particular post. So now we have our category in description. We could have an image for this category if we wanted. We have all our category information there, and then we have a list of post with each one of them having their own information. And as I mentioned and sort of have been harping on, we're using the same function so that that redundancy from before is now benefitting us here as we can use the same function to display both the data about the category and the global sense, but also weaken displayed inside of our loop and patches passing the object. And we don't have to rewrite a bunch of functions or sort of do all of that code in here. We're using the exact same functions to display data, and I really think that's the best way to go about doing your template ing. It makes thes all of these functions highly reusable and makes writing your actual template . Pages like this pretty easy because you're just calling functions that are built into the frame work at a deeper level. That's really what I want toe sort of focus on with this because the HTML to me is you know they smell can be whatever, depending on how you want to write it. It's the PHP code. It's the way that you set it up the way you bringing everything together that really makes this useful. So anyway, that is how to create our our category page course. We still have to do our home page, which is what we're going to do throughout the rest of this course. 13. Build the Homepage Template: All right. Now with our category page out of the way, we can finally get to our home page. So we click over to home and see we haven't done anything. Ah, with this yet. So we can go ahead and do that. I'm gonna sort of clean some of this stuff up here just a little bit, and we will finally come down to our home dot PHP down here. Now, it's nice about this is as we've been going through this we've been writing all of these functions Will. Now we're sort of at the point where we kind of have a lot of that back and stuff written, and we can just sort of create our template. And so I'm gonna make this Ah, a little bit easier on us and just do this. And we know what all of this code is, right? We've done this several times now, so we're including our header were including our footer. Ah, The HTML to me is mostly relevant, as I've said, because it's driven by bootstrap, but we're creating essentially a container. We're creating a row. We can actually get rid of this because we're putting that in the body we don't need. That would be template home anyway. But recruiting a container or creating a row, we're creating our eight column with column. And then remember our four Calm with calm and our sidebars alongside of that inside of our row. We're now getting our posts. Were getting a list of all of our posts were looping through our posts. We are creating a another container that's 12 columns wide. We're creating our blawg post here. Text left. We have our block post title were linking to the title of the post. And with get Perma link, we're passing in our post object Because again, remember, we're on the home page. So if we don't pass in our post object that we created from our loop here, it'll grab the information about the home page, not the individual posts that were looping through at this moment. So get Perma link and then we're gonna display the titles were linking up the title here. Date author, just like we've done before. It has an image. We're gonna display the image with a partner link. Where do they splay the expert excerpt? Ah, the read more button with the Perma link almost identical to what we just did with categories except instead of getting category posts were just getting all posts here. Okay? And so if we come over and take a look at this now, what we have is we have a list of all of the posts on our site. And that is our homepage we go to about We have our about page. If I click on a category, I get a list. The category goto post. I get that latest posts. Take me to those individual posts. Same down here. You know, these were taking us to those categories to those posts and so forth. Go back home. Ah, so we have sort of a full functioning blawg. And then if we want to come over here and we take we come into our content, let's go ahead and close up menu and page And let's open up, Ipsum, Laura and all we gotta do to create a new posters. Copy this and we will create a new file. And let's just call it Hot Dog. Ah, just to have something random, right? So what's the title of post? Well, we'll give it a pilot title of Hot Dog. What's the slug? The slug needs to match the file name. That's on Lee Thing that really matters So hot. Dash dog, Hot Dash Dog authors me will do. Ah, 30 and we'll keep that the same. And we'll just do that. The content for this post is hot dog. Okay, so let's go back to the list of our posts. Can see Hot Dog now shows up May 30th. It's on top. It's an order excerpt, of course, because we only have hot dog in there if we click to read. More were now taken to that page with that Ah, information the categories or none. Um, if we wanted to add this to a category, we come into category and let's say we want to add it to this Ah, demo So we would create a We do a comma and we would do hot dash Dog and that will add it to this demo category. We could add it all of these if we wanted to go and refresh that now its added to the demo category. You see, it's listed in the demo category, etcetera, so you can create all of your content that way. Ah, and it's really straightforward. And again when you're creating this content right down here, you can use Marca. You can use mark down in order to actually have it all be formatted properly and so forth. So we've got a full template that will allow us to create content pretty easily, pretty quickly. Used, marked down. And we don't have to use the database Ah, at all. 14. Create a Custom RSS Feed: All right. Now we can dive into the RSS false. Let's go ahead and close up our categories here. Let's open up our RSS template, and then we need to create a new file in our library, and we're gonna call that rss dot PHP. And then, of course, we need to go to Index. And we need to include our that new RSS file, so we will drop that in here. All right, so there's our RSS file, including get rid of that. Um, and then we can open up this RSS stop PHP file here. So we got one. Which is gonna be in the template. This is going to this is you know, you can you can sort of edit this inside of the template here. We could maybe put this somewhere else and not not necessarily do it this way. Um, but you'll as you'll see here, it's a pretty straightforward file. So it's not a huge, huge deal. So what we need to do in the actual template file, which is going to get loaded by our system? If it's an r s, remember, go back to when we built the Rourke class. If it's an RSS feed of it to text RSS feed that is going to display this RSS template falls We're gonna do header and we're going to do said the content type and we're going to set it to text slash xml because RSS feed is an XML file and then we're gonna sit set the charge set equal to utf eight. Ah, header to truth. And then we're gonna call this function called generate RSS feed. And that is that's all we're going to put in that template file because we're gonna come over here to our file in our library and this is where we're going to actually generate or rss feed. So we're gonna do PHP were do function, generate RSS feed, and we're gonna close PHP and then open PHP. And we're just gonna build an XML file in here. So there's two sorts of things here. There's the actual XML file, which, you know I recommend if you're gonna build an RSS feed doing some, I'm gonna go through it a little bit here. But if you really want to get into detail with it, then I recommend you read more about how our XML files and specifically RSS feeds are generated. So this first little block here, I'm just gonna drop in. I don't think there's a huge need, um, too to write all this out. But the first thing we're doing here is we're telling it that this is we're saying this is an XML file text, mental version one and encoding is utf eight RSS or using version two And then these air also essentially defining the standards, were going to use for different things that were going to include in our RSS so content of the comment AP I If you're gonna have comments, elements, the Adam standard syndication slash on then Yahoo RSS So these air again just different things that we might include in here. I don't think we actually use all of them, but they're included in here in case you want to use some of the stuff on the's, essentially make it so you'll see somewhere we use some of them, but they make it so you can specify different data for a particular item. And the RSS reader that's reading it can use this this right here to reference the standard and know what you're talking about when you include that item that is not a part of sort of the standard to fall RSS stuff. Okay, so again, as we go through this, that shouldn't make a little bit more sense here. So Ah, that is That's what we're doing that here and you'll notice we're doing that inside of our opening RSS tag. And of course, then we'll have our closing RSS tag a swell inside of that. Then we're gonna have our channel information. So we're gonna do channel. And this is where we're going to include information about our channels. We're gonna have title and actually, let me just go ahead and drop this in a swell is just with this RSS feed. There's a lot of code here, so coating all of this out is less important and more about understand what's happening. So we have the title we're using. This is the title of the site. OK, so this is about the channel, which is the site as a whole individual items. So we have the title of the site while we're using our get site info. Ah, function toe pull that in and just echoing it out. We want the link. So were we were doing Ah, print f and we're doing a placeholder slash feed. And what we're putting a placeholder is our home. Your else. So it'll be home. Your l slash feed the relationship itself. So we're talking about this particular This is a link to this feed Ah, that we're talking about. And what type is it? It's an RSS XML file. Ah, the link again, This is just a standard xml Ah, element here. So the home, your l description using get site info to get the description last build date. And so what we're doing here is we're just We're creating a for a date format that Ah, that this element wants so that this is the format that's the RFC to to whatever specifications and then, ah, we're doing the time now. So basically, whenever it gets loaded is going to say that as the last Bill date because that was the last time this RSS file was built was the time now when you just loaded it. Language in my case is English us. And then you see this s y and actually appear we have Adam, you So you see these up here. We have XML and S Adam XML and S S y. So this is whatever is reading this. This is how it will know. Okay, you said you you prefix this with Adam. Well, what standard is that? Well, Adam is this standard right here. And so then that gives it information about how to read this and what to do with this information. Same with this s y. So the syndication is update, period. How often should any reader that is reading this update it? So it's hourly, and then the frequency is everyone. Our So it's saying update this, everyone. Our You could have a course change that if you wanted Teoh. OK, so that's our channel information. We're going to stay inside that, and now we're going to build our items. And so this is just simply going to be a list of our post. We're gonna PHP posts equal, get posts. I and so that's just going to get a list of our post. We've already written that method or that function, so we don't have to do that anymore. So then we're gonna do, if not empty posts. So if it's not empty then we're gonna loop through it for each posts as posts. Then, of course, down here will do, uh, and for each so A and r for each loop and again that is inside of channel and then inside it here, we can actually start building our items. So will do item. And then this is all the information we're going to include about this particular item. So again, there's, ah, a number of things here. But I'm gonna go ahead and drop this in, and then we can we can sort of talk about it. Um and actually, we need to do end if here. All right, so this is again all the item information. So Title Link Publication Date Creator, which uses the d. C prefix which this is the standard for that. Okay, Ah. Categories. If we have categories. Ah, the G u I. D. Description, the content, any enclosure, and then any enclosures. Okay, so again, we're using a lot of the functions already built for title were using the title, and we're passing in post again. This is the same thing as before, where we were loading the RSS page, so to speak. And so if we use the global object, we would have information about the RSS page. Now, if you remember back to when we built a class Ah, we actually didn't pass any in for any data content information to this page. So would actually, if we didn't pass in an object to these functions, it would just be blank. Um, so that's why we have to create this loop and then use. And actually, that wouldn't work because we need toe do that as Post. But we're using this post object from get posts as we looped through it. So the title link word is the Perma link to that particular post. The publication date is date, and then we're setting the format that the RSS readers want and we're doing string to time and they were doing get the date from the post. So you remember we had the the date function that we created and used every else, but that was built on top of get the date. Well, here we need to reform at the date and in a way that we want. So we're just doing get the dates so we can use it in PHP and then we're using string to time to turn that string that is saved in our content files into ah, UNIX timestamp. And then we're reformatting it back into the RFC specifications that the XML or the RSS needs. Okay, so that's what we're doing here. The creator is the author on them passing in the Post object, and then we're doing post categories. So we're gonna get post categories, which was one we've already created. We're gonna use post slug from our post object appear. If it's not empty, then we're gonna loop through. And this will create a category element for each category that this post belongs to and what we'll do. Here's will echo out the post category and then the name. So that'll keep basically created a list of categories to tell it. What categories this belongs to. The G u I. D is just the Perma link with the post object past and description. Is this the content content encoded? Because in order for certain readers and so forth, in order for it show up need this content encoded? Um, the sort of the way it works is this description was never meant to in the beginning was never really sort of meant to take tags and like the full content. And so there was this content encoded that was created for that purpose. But then, at some point description was upgraded to where it could take full content something anyway , Generally, you'll find both of these in RSS feeds. They both have the same thing in him. You see, they both have the content in here, so But we're just echoing out the content here. If it has an image, so I can check and see if it has an image, we're gonna create an enclosure, and we're going to for the u. R L We're going to use get the image, and that gives us the image you are. I'll get the image returns to the U. R L to the image for this particular post. Ah, and then the RSS readers and so forth. They're gonna want the length and type of the enclosure. And so we actually need to We could potentially do this manually in a way, but I didn't want to do that. So I wrote to functions called foul sized girl in mind type of oil that they're gonna go get this dynamically. So for each image, it's gonna figure it out. What? The foul sizes and one that might type is so in order to ah, do that, we need to go over to our utility file here. We'll come down here and racks and going to create a few different functions here. So we're gonna do function, get header, and we're gonna have We need a you, Earl, and then the header that we want. Okay, so this is gonna look at a file and get a specific header that we want. So we'll do Head equals array. Ah, change key case. And we're going to do get headers, which is a PHP function will pass in the URL. I'll set this to true. All right, so let's go ahead and look at get headers. So if we look at that first primers the URL format, if the optional format parameters set to non zero get headed parses this response and sets the arrays keys Okay, so that's what we want. We want it parsed, and we want it set the key set so we can ah actually grabbed the data that we want a racheting cici change Key case Just changes these. If we come down here, you'll see that the headers are all Ah there in their upper case like this. So we generally in PHP you don't you don't. Ah, that's not how you work with, um Ah, Ray keys. So we just run a ray change key case on it to change them all the lower case. So it's more like what we're used to write. So that gets the header. Ah, and now we need to get the specific gets the full head. Or now we need to get the item that we want. So we're gonna do header as equal to head and we're going to do header That was passed in. Okay, so this is going to go into the array that was just created by get headers and it's gonna grab the specific header that we passed in here. If we look at our RSS file here, um, we come over here. Well, actually, we're not We're not passing it there. We're gonna pass it in. We'll see that in a second. We're gonna actually pass it inside the other two functions that we're creating, but you'll pass in the header that you want. So, for example, do you want content type? Well, that's one that we want. We want content length. That's the other one that we want. So that's gonna help us t get the information that we want. So that's gonna get us the specific header that we're after. And then we're just going to do if ah, uh, empty. So if that header isn't set, then we're going to return false. Otherwise, we're going to return the header. Okay, so this is going to get a set of the reason we're doing the header is because this information is included in when the when the file is sent. This all of this information about the file that we need about the mime type in the, um the size are sent in the header. Right? So that's going to get the header that we want. But now we need to We still have our file size. You are on our mind type your l and so forth. So what we're gonna do is we're gonna critic function called file sighs. You are l. And we're gonna pass in the URL that we want, and we're gonna set file size equal to. And now we're gonna call get header The function we just created We're gonna pass in are you Earl? And we're gonna tell what we want. Content length, Okay. And then if it's empty, we're just gonna return zero because we don't know what it is. We'll just say it zero and then otherwise, we're gonna return the file size. So what this is going to do is it's gonna go in and it's gonna grab content length. And it's gonna that's what it's gonna return 438 so that Ah, no, that's going too. Allow us to get the file size for this, and then we're gonna output that toe RSS feed. Yes, you probably should think about it here. Escape this in some way. In this case, you could run isn't on it to make sure it's an imager. And if it's not an energy just returned zero. So if empty file size or not, uh, is, I think it's isn't I mean just double check real quick. Eso isn't so. This will just whether it's an image or not so true and false, if not so we could run, isn't on this and do file size. So if it's empty or it's not an in a church going to return zero and then otherwise is gonna return the file size and again over in our RSS file were calling file size you earl on the image. So it's gonna look at the images headers, Look at the content length, grab that header check if it's not empty or if it's not an imager. And if it is both non empty and an imager than it is going to return. Whatever that header says is the file size. All right, so that's that's that one that's file size, you earl. And then we also have function. Ah, mime type you are. Ah, all right. And we're gonna set mime type equal to get Header, and we're going to do content type and again, we're going to check to see if it's empty. It's empty. Then we're just gonna return false. Otherwise, we're going to return the mime type again. You could look at, you know, maybe there's some other checks you want to do on this, just like we did on the one above. I don't necessarily know what those are off the top my head Maybe escape this because some of this stuff could be spoofed or it could be wrong and so forth. So, um, but in this case will leave this sort of how it is right now on this particular one is now we have mime type. You are l we have file size. You are elf. We go to our RSS file over here mime type euro that's gonna give us than the information about those. So ah, you know, and the other thing is is with the like, sort of a security part of all of this is and I know this isn't necessarily 100% the answer , but you're not having this isn't something that you have a front end interface where an admin is gonna be uploading stuff. This is all you're gonna be adding this all in the back end as the developer. So the images you upload, hopefully you're not trying to hack yourself. Ah, So, generally speaking, you're not gonna be at a huge risk for someone you know, trying to upload something. And there being a problem again, I know that's not ah, not 100% the best way to go about security. But it's why I don't feel so not doing anything with his mind type euro, for example. I don't feel like it's as big of an issue. If it were as it would be if you this were something that had a front and in your pay face that was accepting user input. There is no user input in this entire system. OK, so it's a little bit different than what you normally talk about when you talk about this sort of stuff. All right, so that gives us than our enclosure are length are type. Are you Earl? Etcetera So and then we close our item. We end our loop, we close our channel, we close the RSS, and then we finish off our PHP over here. So if we take a look at our file over here and if we click the RSS feed, we should get and you see, we get ah, we get a list of our files here. So what we end up with is ah, an RSS feed that a fully functioning RSS feed that we can confuse and people can subscribe to and so forth, and that we, of course, put that link up over here in the menu. So again, that's how the RSS feed works. This is a pretty standard RSS feed file, and then we've just taken it. And we've used our framework to be able to build this out and that that's ultimately how you build on our SSV file. It's nothing no more complicated than that. So hopefully learned from something from that and and are able toe build this out in this system or whatever system that you might, you might be using something like this for 15. Questions: All right. So that'll do it for this course. Thanks for riding along with me again. I hope you'd learned a ton as you went through this. Um, if you have any question, be sure to ask those. I will. Ah, create follow up videos asked answering some of the common questions that I might get. If you have any of those are going over maybe things that aren't clear, whatever that is, just let me know, and I will do that also. Ah, if you would go ahead and follow me here on skill share, I'd really appreciate that. Will also help you to get notified when I post new courses. So if you want to keep up to date with all the courses Ah, that I'm posting them. Be sure to follow me on here as well. All right. That will do it for the course. Thanks for riding along, and we'll talk to you in the next one.