SitePoint Sponsor

User Tag List

Page 1 of 16 1234511 ... LastLast
Results 1 to 25 of 397
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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.

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a simple, basic implementation of the Front Controller pattern in PHP.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 Code:
    <?php include('common/header.php'); ?>
    <h1>news</h1>
    ...
    <?php include('common/footer.php'); ?>

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    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

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    news/news.php
    PHP Code:
    <?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
    PHP Code:
    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:

    PHP Code:
    $page =& new Level2Page($_GET['page']);
    $page->option('display_date'false);
    $page->display(); 
    Level2Page would know how to handle the logic.

    - matt

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Anyways, end of nag (for the moment).

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cadmiumgreen
    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...

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

    PHP Code:
    $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:
    PHP Code:
    function handle_news() {
       
    $news get_all_from_db("SELECT * FROM news");
       include 
    "templates/template_for_news.php";

    hope that helped

  9. #9
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And how about:
    PHP Code:
    $controller = & new Controller($_GET['content']); 

  10. #10
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  11. #11
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    PHP Code:
    $handler(); 
    You can replace that with this:

    PHP Code:
    $layout "layouts/main.php";
    ob_start();
      
    $handler();
    $content_for_layout ob_get_clean();
    include(
    $layout); 
    With the main template:

    HTML Code:
    <html>
    <body>
    <?php echo $content_for_layout ?>
    </body>
    </html>
    And the news template:

    HTML Code:
    <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 Code:
    <?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 Code:
    <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
    HTML Code:
    <?php $page_title = 'News' ?>
    <h1>news</h1>
    ...
    Hello World

  12. #12
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Why, in this day and age it still goes on I don't know as there isn't any excuse for it.
    PHP = template language + fuction library (+ some OO in PHP5)

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?!

  14. #14
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Hello World

  15. #15
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    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.

    It's just shocking
    Why is

    Code:
    <html>
      <head>
        <title>
          {CUSTOM_TITLE_TAG_HOLDER}
        </title>
      </head>
    </html>
    better than

    Code:
    <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.

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing wrong with
    Code:
    <?php ... ?>
    but something wrong with this on the other hand,

    Code:
    <html>...<?php ... ?> </html>
    PHP is a buitiful language, so lets not degrade it with something as simplistic as HTML (Can just imagine the response I'm going to get now...)

  17. #17
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    PHP is a buitiful language
    That sums it up
    Hello World

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    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

  19. #19
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    What I can't understand is why people have the need for putting PHP in a template file anyways?
    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.

  20. #20
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    Why is

    Code:
    <html>
      <head>
        <title>
          {CUSTOM_TITLE_TAG_HOLDER}
        </title>
      </head>
    </html>
    better than

    Code:
    <html>
      <head>
        <title>
          <?php echo $title; ?>
        </title>
      </head>
    </html>
    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).

  21. #21
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think we have been through this before. There is nothing wrong with using PHP in templates unless you can't or don't want to trust the template designers. Because there is a difference between:

    {CUSTOM_TITLE_TAG_HOLDER}

    and:

    <?php readfile('/etc/passwd'); ?>
    Christopher

  22. #22
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    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.
    PHP syntax is not the same thing as PHP logic. You need to include SOME syntax into a template, right? Why invent a completely new one when we already have a perfectly good one?

  23. #23
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    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).
    Re-read what you quoted

  24. #24
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because a) it's faster,
    As I recall, an extract and include is actually slower than a file read and str_replace in most cases.
    you don't have to invent a whole new language to use it (whether as simple as {} or as complex as Smarty).
    Certainly there is no prohibition against creating domain specific languages? People much smarter than me use and recommend them all the time.
    Christopher

  25. #25
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Presentation logic in a template file does not break layering.
    That is true in some (most?) cases, but now that we have such a thing as the ViewHelper pattern, why not just separate out the presentation logic anyways?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •