Page 1 of 1

Best way to code +header/footer style

Posted: Sun Jan 29, 2006 12:19 am
by Jim_Bo
Hi,

Which is the best way to code using header / footer style .. ie

Just add

Code: Select all

$pagename = 'Home Page'; 

require_once 'header.php'; 

content here 

require_once 'footer.php';
Or using case:

Code: Select all

require 'header.php'; 

switch ($_REQUEST['page']) { 
                         
        case 'about': 
            require 'data/about.php.php'; 
            break; 
             
        case 'contact': 
            require 'data/contact.php'; 
            break; 
                         
        default: 
            require 'home.php';         
} 

require 'footer.php';

Thanks

Posted: Sun Jan 29, 2006 9:36 am
by jwalsh
Theoretically, as I see it, both examples are identical. The only thing changing is how you parse the internal content.

I prefer a OOP display class, but many people here will argue with me on that one :lol:

Posted: Sun Jan 29, 2006 9:46 am
by timvw
Imho they are both _very_ different..

The first example has the approach where the specific code includes the common code...
The second example has the approach where the common code includes the specific code...
(In OOP this problem is known as has_a versus is_a)

I choose for a third approach, a mix of both (like .net masterpages or java tiles) This way you can easily change the "common" part but without that all your pages are dependant of it. (Offcourse there are OOP implementations of this approach possible)

Code: Select all

<?php
// specific1.php
$page['body'] = '/specific1.html';
include('common.php');
?>

Code: Select all

<?php
// specific2.php
$page['title'] = 'some title';
$page['body'] = '/spefic2.html';
include('common.php');
?>

Code: Select all

<?php
// common.php

// load default values if they aren't overriden
if (!isset($page['title']) $page['title'] = 'default title';
if (!isset($page['body']) $page['body'] = 'default_body.html';

header('Content-Type:  text/html');
?>
<html>
<head>
<title><?php echo $page['title']; ?></title>
</head>
<body>
<?php readfile($page['body']); ?>
</body>
</html>

Another example of masterpage: http://timvw.madoka.be/?p=431
And a possible OOP implementation: http://timvw.madoka.be/?p=426

Posted: Sun Jan 29, 2006 9:59 am
by jayshields
Sorry, I don't understand the above post..! Edit: OK, re-read it, but as I see it, it's the same as the very first example but more long-winded is it not? Edit 2: OK, looked at that article on your website, seems like a good idea if you want to take extra precaution, but personally I wouldn't go to that much effort. Find & Replace across all files you want to remove rightpanel from will get the job done quite easily enough.

Anyway, I do it like your very first example.

That case system looks quite intuitive, but I've never used $_REQUEST before, is it the same as $_GET? ..or should I just go and find out for myself instead of asking silly questions...?! :lol:

Posted: Sun Jan 29, 2006 10:28 am
by timvw
about: $_REQUEST

The use of a switch has the disadvantage that that switchpage has to know _all_ the pages that are available..
There used to be a nice article on phppatterns.com about view building strategies but i can't find it back...