Urban75 Home About Offline BrixtonBuzz Contact

Using PHP includes for web page templating

DG55

Well-Known Member
I have been looking for a really really simple way of templating pages on web sites for ages. I've seen content management systems like "limbo"? etc, but they are just too complex, and I've always had a bad experience with them.

So I did find somthing that works and is as simple as I thought, and infact i've always known about SSI stuff, just never thought I could use it this way.

Basically im taking all the <head> stuff, and in the body im taking the header and footer stuff, and for all three im creating seperate html film with that global code in.

Then in the content pages, all that remains is the actual content, e.g. the text and images of the pages. At the start and end of the body, and inside the <head>, Im just using -

<?php
include ("header.html");
?>

So this adds the relevent code when the page is loaded. Very lightweight, and extremely easy if I want to, for example, add just one entry to the navigation bar.

This solution seems perfect, so easy to change, and so easy to implement.

However the question, is whether this is all good to go. I know all browsers (on windows anyway) seem to render the pages fine, but perhaps thats just luck, is there any problems with creating pages like this, is it good web practice etc?

Thanks.
 
Using PHP includes is a perfectly normal and standard way of creating a site; you just need to make sure that the server on which the site will be hosted eventually supports PHP.

It's good working practice to put all your included files in another folder called /inc or something similar to keep them separate.
 
Good stuff!

Thanks for the advice.

Yeah I have PHP on my server, hard to find servers without isnt it?
 
You don't really need to use php though do you? Surely
Code:
<!--#include file="included.html" -->
or
Code:
<!--#include virtual="/directory/included.html" -->
(for absolute paths)
works just as well?

I have no idea which is heavier on the server, php or ssi includes...

But yes, it's a standard method and one that I like.
 
For something a little more formal, you might want to look into the smarty templating language. It's pretty powerful and allows you to seperate out the php code and the web layout.
 
fractionMan said:
For something a little more formal, you might want to look into the smarty templating language. It's pretty powerful and allows you to seperate out the php code and the web layout.

Are there any online comparisions between this and other templating mechanisms...? I'm fairly new to php and need to look into this for a project... (Yep, I know about Google but I was wondering if there any good guides...)
 
Crispy: you could just use SHTML, but you've got so many more potential options available with PHP that I don't think it's worth it unless you're sure you're only going to ever want to just stick in dumb HTML. For most sites the server load isn't going to be noticeably different.

I used to use SHTML for all my pages but I prefer PHP now; the default template does smarter things like create a breadcrumb trail based on directories.
 
Yeah I used the SHTML ones, but then I had some problems with the when the files werent parsing, never had any problems with PHP though.
 
Crispy said:
I have no idea which is heavier on the server, php or ssi includes...

I'd guess the php with it needing to be interpreted but I doubt there's enough in it to worry about. You can shave an eentsy amount of server work off by using single quotes instead of double (single doesn't allow interpolation), e.g.

include ('header.html');
 
I just wondered what happens when you have links in the template that your including.

e.g. if you have a link in there like /images/image1.jpg

Does the link start from where the template is or where the page is.

Err, hope that makes sense.

Im just wondering if there will be problems with putting different pages in folders.
 
The file refs in the generated HTML are just standard HTML file refs, relative to the location of the original page.

The filenames in includes are filesystem ones, not web server ones. If you have

include "/includes/header.php";

it will look in the root directory on the server itself, not wherever the directory holding your pages is. These two aren't going to be the same.

You can make things easier by using something like

include $_SERVER["DOCUMENT_ROOT"]."/includes/header.php";

which will automatically add in the right location on the server's filesystem.
 
Hmm well i've got

<?php
include ($_SERVER["DOCUMENT_ROOT"]."../inc/header.html");
?>

The include doesnt work at all though.
 
Hmm are we talking about the same thing? I think thats just a fix so you dont have to add ../../ to get to the include file, but thats not really a problem, although handy perhaps.

The problem is having links + images inside the header.html include file.

So a document inside a folder only links an image /images/image.jpg instead of adding the ../images/image.jpg to find its way.

Also, wont $_SERVER["DOCUMENT_ROOT"]. take me right back to the default folder on my server? I really have no idea how that works anyway.
 
jæd said:
Are there any online comparisions between this and other templating mechanisms...? I'm fairly new to php and need to look into this for a project... (Yep, I know about Google but I was wondering if there any good guides...)
Not that I know of. Smarty is pretty good though. It compiles templates into PHP pages, so it's quick too. Easy syntax to learn, reasonably powerful and quite flexible (for example, you can create user defined tags for use in templates)
 
Basically if I use the same template for pages which are on different levels, for example

index.php
photos/index.php
photos/london/index.php

And in the template I have:

<div id="navigation>
<a href="photos/index.php"><img src="img/photos.gif"></a>
</div>

Obviously if that renders inside photos/london/index.php, its going to be linking to:

photos/london/photos/index.php

So ultimately I need a ../../ in there only on that certain page.

The only way I can think to overcome this is if I put in absolute links, but thats not really realistic is it. Imagine a navigation bar with two design images on there, 6 links with images, thats a helluvalot of wasted code.

Im sure theres somthing you can do inside the template to make the links go to the default route everytime and then link up from there.

Otherwise Im guessing in templated sites you have to put all your php content files in the same folder, gah!
 
Cheers.

I already found a solution, simply putting a / infront of the link, that seems to fix it to the root...

Not sure if that one is better?
 
Everything in your HTML comes out relative to the location of the original page. You're only producing one page. The browser doesn't care what the location of things that you call to produce it are.
 
DG55 said:
Cheers.

I already found a solution, simply putting a / infront of the link, that seems to fix it to the root...

Not sure if that one is better?
That works as long as you don't create any sub-folders - as soon as you want to expand you'll have to change.
 
Really? Because I basically have...

index.php
and everything else in a subfolder e.g.
photos/index.php
contact/index.php

all running off the same template file, and it works

the links are just
/photos/
/contact/

etc.
 
Here is the code I use on my index file to include content for my site. It uses the simple switch/case control method. The if statment is just to work out which footer to use for each page. The data is sent to the script via the url link in the $_GET global array.

ie:

http://www.mysite.com/index.php?id=links (or./index.php?id=links).... if I want the link to show my links page

I keep the header and footer in the top level directory and all the page content in a subdirectory, making editing easy.

you could even drop the .inc files into a mysql database and use a more advanced index file to get them when needed. I did this for a work project and included a back admin section so that pages could be edited ina web browser.


<?php

#decides which footer to use#

if ($_GET['id'] == "home"){
$footer = "home_footer.inc";
}
else{
$footer = "footer.inc";
}

#include the header#

include ("header.inc");

#the control switch statement#

switch ($_GET['id']){

case "home":
include ("includes/home.inc");
include ("$footer");
break;

case "audio":
include ("includes/audio.inc");
include ("$footer");
break;

case "links":
include ("includes/links.inc");
include ("$footer");
break;

case "parties":
include ("includes/parties.inc");
include ("$footer");
break;

case "contact":
include ("includes/contact.inc");
include ("$footer");
break;

case "eflyer":
include ("includes/eflyer.inc");
include ("$footer");
break;

case "lineup":
include ("includes/lineup.inc");
include ("$footer");
break;

case "charts":
include ("includes/charts.inc");
include ("$footer");
break;


default:
include ("includes/home.inc");
include ("home_footer.inc");
break;

}




?>

as you can see, it is a doddle to add new pages, just add another case statement.
 
DG55 said:
Cheers.

I already found a solution, simply putting a / infront of the link, that seems to fix it to the root...

Not sure if that one is better?


./ is better for defining root, especially if the file that is calling isn't in root itself.
 
tendril said:
./ is better for defining root, especially if the file that is calling isn't in root itself.
That just refers to the folder that the file is in, rather than the absolute root of the site, doesn't it?
 
tendril said:
as you can see, it is a doddle to add new pages, just add another case statement.

Well... Its a bit long-winded and since the page name and html files match I would've just stored valid pages in an array. A lot less typing...!
 
Back
Top Bottom