A sample skeleton script that I’m using for some of my websites would look like this…
<html>
<head><title>A Website</title>
<!-- other junk and css goes here –>
</head>
<body>
<!-- header –>
<!-- body –>
<?php
switch ($_GET[‘content’]) {
case (''):
echo (require 'news/news.php');
break;
case ('news'):
echo (require 'news/news.php');
break;
case ('profiles'):
echo (require 'profiles/profiles.php');
break;
case ('links'):
echo (require 'links/links.php');
break;
case ('help'):
echo (require 'help/help.php');
break;
default:
echo (require 'news/news.php');
break;
}
?>
<!-- footer –>
</html>
I’m not that new to PHP, but I’m not that proficient, moreover, I would like to know the general practice that most PHP developers use when designing a website that generally has the same menus, but the content changes.
For all websites out there, how are these HTML/PHP scripts usually written?
Would PEAR help at all?
Thank You so much for your time,
cadmiumgreen::Byron M.
A big improvement over your design, would be to seperate controller (the switch-statement) from the view (the html-markup). This could be something like the following ; index.php
<?php
switch ($_GET['content']) {
case 'news':
include('news/news.php');
break;
case 'profiles':
include('profiles/profiles.php');
break;
case 'links':
include('links/links.php');
break;
case 'help':
include('help/help.php');
break;
default:
include('news/news.php');
break;
}
?>
switch ($_GET['content']) {
case 'news':
include('news/news.php');
break;
case 'profiles':
include('profiles/profiles.php');
break;
case 'links':
include('links/links.php');
break;
case 'help':
include('help/help.php');
break;
default:
include('news/news.php');
break;
}
The Chain of Responsibility (classic Gang of Four) would offer greater freedom in this regard I think? For more on the CoR and an example (or two), do a search for ‘Design Patterns Catelog’ in this forum, for the thread I started
This method makes the content page less re-usable. It’s bound to the common/header.php and common/footer.php files, which may or may not be OK. Why not just a simple function that contains the logic within the requested page.
index.php
includeTemplate('header'); // renders the 'common/header.php' file
includeNewsContent($_GET['page']); // handles logic, error checking for content file
includeTemplate('footer'); // same as above...
And those functions would contain the extra data for extentions also, removing repeated code. THis isn’t ideal but if I were going to make it simple, I’d probably start off with this and re-factor later to something better if needed. Personally, I usually create a class called StandardPage or something that handles all of that stuff. Then in the requested script (index.php, page.php etc.) I would instantiate it and customize as needed:
$page =& new Level2Page($_GET['page']);
$page->option('display_date', false);
$page->display();
For simplicity, what I do is to read in the template file, looking for specific tags, which are replaced with dynamically generated content. So there isn’t any PHP at all in the HTML, and this is for simple applications, web sites, etc where there is limited HTML fragments.
What I can’t understand is why people have the need for putting PHP in a template file anyways? Why, in this day and age it still goes on I don’t know as there isn’t any excuse for it. But for what it’s worth, PHP is not the only language that suffers from this
For simplicity, what I do is to read in the template file, looking for specific tags, which are replaced with dynamically generated content. So there isn’t any PHP at all in the HTML, and this is for simple applications, web sites, etc where there is limited HTML fragments.
What I can’t understand is why people have the need for putting PHP in a template file anyways? Why, in this day and age it still goes on I don’t know as there isn’t any excuse for it. But for what it’s worth, PHP is not the only language that suffers from this
Please, no offence, but haven’t you paid any attention to all these PHP-is-already-a-template-engine debates?
Note that the template can define variables too, so if it wanted a specific layout file it could set $layout itself. (Or call a function to set that if you want a little more abstraction.)
Douglas
The code so far:
index.php
<?php
function handle_default() {
handle_news();
}
function handle_news() {
$news = get_all_from_db("SELECT * FROM news");
include "templates/news.php";
}
function handle_profiles() {
}
function handle_links() {
}
function handle_help() {
}
$content = @$_GET['content'];
$handler = "handle_$content";
if(!function_exists($handler)) {
$handler = "handle_default";
}
$layout = "layouts/global.php";
ob_start();
$handler();
$content_for_layout = ob_get_clean();
include($layout);
?>
layouts/global.php
<html>
<head><title>A Website <?php echo $page_title ?></title>
<!-- other junk and css goes here -->
</head>
<body>
<!-- header -->
<!-- body -->
<?php echo $content_for_layout ?>
<!-- footer -->
</body>
</html>
Yep but there is such a thing as layering, and separating those layers. It is surprising and disappointing to realise that there are developers out there, who use OO and design patterns every other day, and still put PHP in HTML.
but haven’t you paid any attention to all these PHP-is-already-a-template-engine debates?
No offence taken, I sure have read all about PHP being a templating engine, but regardless of using either a prodecural or OO approach, to me it just doesn’t fit well, to have PHP in HTML.
For example, just what are the overheads of replacing <bodycontentwouldgohere /> with the pregenerated HTML from elsewhere? None, basically Like, there must be about 101 different ways you could implement this sort of functionality?!
It’s called presentation logic. There’s nothing wrong with it. It’s what php was originally designed for. Presentation logic in a template file does not break layering.