Website "templating": Make updating your site fast and easy.
Introduction
This method of developing your website is best used early on in the development process. Once past the early stages it can be time consuming and sometimes confusing if you impliment this feature too late in development. So, lets say you've just started your site and maybe you have a basic HTML page with some basic stuff on it like a header, a side bar, a content area, a navigation area, and a footer. Lets pretend you make a second identical page with all the elements of the first, and then want to change one line of text in the side bar area. Normally you'd have to go to both pages and edit that line, re-upload them through your FTP and refresh your page. Well, you're still got to do the last 2 steps, but I've simplified the first step into a 1 edit process. This tutorial makes use of extremely basic PHP, so you may even learn something you can use for future sites as well!
The One Step Process
Say you've got something like this, you've got all your elements on one page and all nicely laid out for you. We're going to destroy that, so instead of your sidebar class you've got "php include();". This may seem pretty easy, and it is. What you're doing is instead of putting actual code you're putting a link to actual code, so with all your linked-to code being in one place (say a folder called "includes" or something) you can easily update every single page of your site at once. This site is put together in a similar manner, and it has made udpating and adding new features or changing old ones much, much easier. All your included PHP files are going to have in them are the exact code you want that file to display. It doesn't need any HTML wrappers, like the body or HTML tag (unless you need it to), and it will display completely normal without them. For example, this is a screenshot of the actual code used to control the navigation bar you see above. All I did was put in the file, which MUST end in .php for this to work, exactly what code I had before I sliced the code apart. In it's place I put "php include(file.php );", which calls the file an displays the code wherever you put it. As a result, the index file is very small as all it has on it are includes.
Recap
Your files should look something like this when you are done:
This is a really simple method, but I've found it incredibly valuable now in the late stages of development. Give it a try, you'll thank yourself later.
148 Views