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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shadow_005
Forum Newbie
Posts: 8
Joined: Sat Oct 08, 2005 1:12 pm

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

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
shadow_005
Forum Newbie
Posts: 8
Joined: Sat Oct 08, 2005 1:12 pm

Post by shadow_005 »

Ah yes that's exactly what I meant. Many thanks.
Post Reply