Building Template-Driven Scripts with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • memset
    Registered User
    • Oct 2002
    • 4

    #1

    Building Template-Driven Scripts with PHP

    I figured some could benefit from this article I recently wrote... hope this helps =). Have a good one.

    For a PRETTIER version go to http://www.mustangmike.org/tutorials..._with_php.html

    Purpose of article: Demonstrate how to build template-driven scripts with PHP.
    Why build template-driven scripts: Most people realize that HTML can
    be used in PHP scripts and actually do embed HTML in their PHP scripts.
    Sometimes though it is far more useful to be able to simply edit HTML files
    instead of sifting through PHP code. By using templates you can very easily
    change the look of your pages without even looking at any PHP code. This
    is especially useful when you work in a team where somebody handles the
    programming aspect and another person handles the design aspect.
    Template concepts: It is very simple actually; you make your PHP script
    read the template file and print it out to the browser. "But what about dynamic
    content?" you may be wondering. The answer is simple: tokens. Tokens are
    essentially just placeholders on a template and are usually signified by a word
    surrounded by two percent (%) signs. For example, you may put a %date%
    token on your template and when it goes through the PHP script it gets changed
    to the actual date. You could do the same thing for member IDs, names, links,
    passing data on forms, etc, etc... the list is endless.
    Lets code it: Not only do I explain the uses and concepts of templates, but I even
    provide working code to make it happen in a ready-to-use format. Below is the
    PHP function I frequently use to display templates:

    #################################################
    function ShowPage ($file, $tokens=array("%no_tokens_passed%" => "%no_tokens_passed%")) {
    // See if the file exists first. If not, return 0.
    if( !is_readable($file )) {
    return 0;
    }

    // See if any tokens were passed.
    $tokens_exist = count($tokens); // counts the keys

    // Open the file for reading
    $fh = fopen($file, "r") or die("Failed opening template");

    // Read the file line by line. Check to see if any tokens were passed before trying to parse any lines.
    while( !feof($fh) ) {
    $line = fgets($fh, 1024);
    foreach ($tokens as $key=>$val) {
    $line = preg_replace("/".$key."/i",$val,$line); // replace all tokens on each line
    }
    print $line;
    }

    // Close the file
    fclose($fh);
    }

    The first argument passed to the function is the actual template filename. Passing
    the full path is the best choice, but isn't necessary. The second argument is optional,
    but is an associative array (hash) of tokens and values.
    An example for using this function would be as follows:
    $template_dir = '/home/virtual/site61/fst/var/www/html/templates';
    ShowPage("$template_dir/member_signup_confirmation.html",
    array("%first_name%" => $_REQUEST['first_name'],
    "%last_name%" => $_REQUEST['last_name'],
    "%email%" => $_REQUEST['email']));
    As you can see, the first argument is the full path to the HTML template called
    member_signup_confirmation.html. This could be a confirmation page where
    the user is sent after filling out a signup for to confirm his/her information before
    submitting. The second argument is a single ARRAY (hash) which contains a
    list of keys & values for the tokens. Inside the member_signup_confirmation.html
    template would be the tokens %first_name%, %last_name%, and %email%.
    Conclusion: You are now fully equipped with the knowledge and resources it
    takes to start building template-driven scripts with PHP so get out there & start
    building =). If you are needing a quick solution and you don't want to code it then
    you can also check out http://www.PHPLabs.com/ for some of the best PHP scripts
    available on the Internet. And while on a completely other topic, you should also
    check out http://www.Cellulean.com/ to see an actual WORKING anti-fat cream..
    even several news stations nationwide have taken notice!
    Copyright Mike Rogne 2005. All Rights Reserved.
    memset
    http://www.Cellulean.com/
    http://www.PHPLabs.com/
  • lb_vee
    Confirmed User
    • May 2004
    • 886

    #2
    This is the idea behind MVC (model-view-controller). Ive modelled a bunch of php bases sites after the mod_perl design utilizing seperation of code and templates while still using only php w/o external templating.

    Here's a overly simple example.

    Basics:
    ----------------------
    Directories are setup like this :
    /lib/
    /lib/model/
    /lib/controller/
    /templates/


    Mod-Alias
    ----------------------
    - Mod alias allows you to have a single template handle all calls based on URI location. so for example I can have /templates/index.php handle everything at '/'. The next step is to setup what I like to call "Page Locations" : www.xxx.com/part1/part2/part3/ where the uri is placed into hash so you get:

    $page["location"];
    $page["section"];
    $page["subsection"];

    - Main.inc (located in /lib)
    This file contains all your includes:
    include("model/user.php");
    include("model/session.php");

    - Models
    The models actually interact with the database. They get, update and set results. So a user.php object model would have addUser(), getUser(), delUser(), modUser()....

    - Controllers
    These items feed data into the templates based on Page["location"].
    So if $page["location"] hahahaha '/contact', then it would get data needed for the contact page.

    - Tempates
    These are your basic templates. the main.inc file is included at the top of the first template that gets called.


    Again, this is a basic example. I dont feel like typing too much right now. If you have questions about this setup, ICQ me and I'll go over it with you.

    Comment

    • shuki
      Confirmed User
      • Aug 2004
      • 3070

      #3
      interesting
      Looking to buy established paysites contact me [email protected]

      Comment

      Working...