PHP
  Home arrow PHP arrow Page 3 - JV’s Power Tips for PHP (1)
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

JV’s Power Tips for PHP (1)
By: Justin Vincent
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2003-01-29

    Table of Contents:
  • JV’s Power Tips for PHP (1)
  • Never Breaking Out of PHP
  • Basic Template Theory
  • Refining the Previous
  • Accessing Other Variables from Within this Type of Template
  • Using Objects to Store Multiple Output Values
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JV’s Power Tips for PHP (1) - Basic Template Theory


    (Page 3 of 7 )

    Back to the above print statement (print <<<END etc.). As a beginning PHP programmer this type of print statement can be used to build simple template driven websites very quickly. Here is a simple template using this concept:

    <?php

    // N.B. This file is stored as ‘templates/simple_template.php’

    print <<<END
    <html>
      <head>
        <title>Simple Template</title>
      </head>
      <body>
        $gBODY
      </body>
    </html>
    END;

    ?>


    Now let's output “Turkey Love” using our new simple_template.php:

    <?php

      $gBODY = “Turkey Love”;

      include_once “templates/simple_template.php”;

    ?>


    This can be improved greatly upon by the use of two simple functions:

    <?php

      // N.B. This file is stored as ‘functions/global.php’
      
      // =========================================
      // Function 1 - Used to append something to a global variable

      function out( $str , $var_name = “gBODY”)
      {
        // Make this global variable available to this function
        global ${$var_name}; // <-- This is a variable variable

        // Add the input of string to the global variable gBODY
        // (or any other specified global variable)
        ${$var_name} .= $str;
      }

      
      // =========================================
      // Function 2 - Used to select a template.

      function use_template ($template_name)
      {
        if ( is_file(“templates/$template_name.php”))
        {
          include_once “templates/$template_name.php”;
        }
        else
        {
          echo “The template $template_name was not found!”;
        }
      }

    ?>


    Then if we place the above functions in a global function file, the whole thing looks much more elegant indeed.

    <?php

      include_once “functions/global.php”;

      out(“Turkey Love”);

      use_template(“simple_template”);

    ?>


    Using this method it is easy to see how we can create any number of templates and store them within the template directory. Let's have a look at this concept using a slightly more complicated template:

    <?php

    // N.B. This file is stored as ‘templates/complex_template.php’

    print <<<END
    <html>
      <head>
        <title>$gTITLE</title>
      </head>
      <body>
        <table width=100%>
          <tr>
            <td width=100>
              $gLEFT_MENU
            </td>
            <td>
              $gBODY
            </td>
          </tr>
        </table>
      </body>
    </html>
    END;

    ?>


    Lets quickly define some global constants in functions/global.php (this will make sense in a minute).

    <?php

      // functions/global.php
      define(“TITLE”,”gTITLE”);
      define(“MENU”,”gLEFT_MENU”);
      define(“BODY”,”gBODY”);
      // etc..

    ?>


    Then to use complex_template.php, the script might look something like this:

    <?php

      include_once “functions/global.php”;

      out(“Page Title”,TITLE);
      out(“My Menu”,MENU);
      out(“Hello World”,BODY);

      use_template(“complex_template”);

    ?>


    If this doesn’t make sense to you go back to the function out(). You can see that any input to out() is written to a global variable (specified as a string in the second parameter of the function out()). Hopefully this makes it clear why I created constants using the define function. Essentially because I’m lazy and when I type:

      out(“My Menu”,MENU);

    It is really the same as typing (with less effort [and more readable] than):

      out(“My Menu”,”gLEFT_MENU”);

    More PHP Articles
    More By Justin Vincent


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT