Proper way to design a 'skeleton' script? More

hello,

how common is it to use a ‘skeleton’ script?

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.

This is a simple, basic implementation of the Front Controller pattern in PHP.

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;
}
?>

news/news.php


<?php include('common/header.php'); ?>
<h1>news</h1>
...
<?php include('common/footer.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;
} 

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 :slight_smile:

Here it is…

Enjoy :slight_smile:

news/news.php


<?php include('common/header.php'); ?>
<h1>news</h1>
...
<?php include('common/footer.php'); ?>

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();

Level2Page would know how to handle the logic.

  • matt

Umm…

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 :slight_smile:

Anyways, end of nag (for the moment).

Generally, try to avoid long spaghetti-style switch statement. In php the same can be expressed much clearer:


$content = @$_GET['content'];
$handler  = "handle_$content";
if(!function_exists($handler))
   $handler = "handle_default";
$handler();

Now, all you need is to implement “handle_news()” , “handle_links()” etc. Note that adding new feature doesn’t require any changes in the code above.

handle_xxx function (aka Controller) typically does some sql stuff (Model) and includes template (View), for example:


function handle_news() {
   $news = get_all_from_db("SELECT * FROM news");
   include "templates/template_for_news.php";
}

hope that helped

And how about:

$controller = & new Controller($_GET['content']);

:slight_smile:

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?

You can replace that with this:

$layout = "layouts/main.php";
ob_start();
  $handler();
$content_for_layout = ob_get_clean();
include($layout);

With the main template:

<html>
<body>
<?php echo $content_for_layout ?>
</body>
</html>

And the news template:

<h1>news</h1>
... 

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>

templates/news.php


<?php $page_title = 'News' ?>
<h1>news</h1>
... 

PHP = template language + fuction library (+ some OO in PHP5)

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 :slight_smile: Like, there must be about 101 different ways you could implement this sort of functionality?!

So you’re saying PHP is a bad language for the template layer? Even though that is what it was built for?

The first line of a PHP file starts with <?php, not #!, because of this.

Douglas

Why is


<html>
  <head>
    <title>
      {CUSTOM_TITLE_TAG_HOLDER}
    </title>
  </head>
</html>

better than


<html>
  <head>
    <title>
      <?php echo $title; ?>
    </title>
  </head>
</html>

Dr. I think you get too hung up on taking things literally and sometimes fail to see the implied meaning of statements.

Seperation isn’t elimination, it’s simplification.

Nothing wrong with

<?php ... ?>

but something wrong with this on the other hand,

<html>...<?php ... ?> </html>

PHP is a buitiful language, so lets not degrade it with something as simplistic as HTML :smiley: (Can just imagine the response I’m going to get now…)

That sums it up :wink:

:lol:

There are a few loves in my life,

  1. PHP,
  2. The other (better?) half,
  3. My Car,
  4. Some beer in the refrigerator

So, that sums it up, yes :wink:

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.

Because a) it’s faster, and b) you don’t have to invent a whole new language to use it (whether as simple as {} or as complex as Smarty).