Page 1 of 1

How does this 'template-like-thing' works?

Posted: Mon Oct 17, 2005 6:14 am
by shadow_005
I see many php sites using either tables or divs in this style:

Code: Select all

---------
|-------|
| |     |
| |     |
| |     |
--------
(The above is a crappy ASCII drawing of a common main page in php :P)

When I click on a link, for example, called "My Profile" then the main div/table contents change but the left (ussually navigation bar) and the top (often containing a logo and some quick links) don't change at all. The page then reloads and it looks like a HTML page using frames... How do they do that (supposing they don't use pre-build packages) or how is that technique called so I can learn about it?
I suppose that they don't write the same 'navigation bar' and 'header' for every page. Do they store a template-like code in textfile/database and retrieve it uppon visiting a page and just change the main table/div code?

I hope you understand what I am trying to ask.

Posted: Mon Oct 17, 2005 7:21 am
by foobar
This has very little to do with PHP, actually. What you have in your contextual navbar on the left depends on what you put in. This can be done with plain HTML (a different menu on each page) or via PHP (same as before, or loading a nav file depending on an argument).

Posted: Mon Oct 17, 2005 7:29 am
by Chris Corbyn
It's generally an easy way to maintain a style across a whole site.

You create index.php or whatever with the navbar and header in it, and a blank area where the content goes.

Then you use a switch construct to decide what content goes in the blank bit using GET...

Code: Select all

<?php

$page = isset($_GET['page']) ? $_GET['page'] : '1';

?>
<html>
<head>
</head>
<body>
<!-- HEADER -->
<div style="width: 100%; height: 90px; font-size: 1.3em; font-weight: bold; border-bottom: 1px solid #CCCCCC; text-align: center;">
    My Header
</div>
<!-- LEFT NAV -->
<div style="width: 20%; float: left;">
<a href="some_link">Some link</a><br />
<a href="some_link">Some link</a>
</div>
<!-- MAIN CONTENT -->
<div style="width: 73%; float: right">
<?php

switch ($page)
{
    case 1: include('foo.php');
    break;
    case 2: include('bar.php');
    break;
    default: include('start.php');
    break; 
}

?>
</div>
</body>
</html>

Posted: Mon Oct 17, 2005 7:44 am
by shadow_005
Ah yes that's exactly what I meant. Many thanks.