Home arrow PHP arrow Page 3 - JV’s Power Tips for PHP (1)
PHP

JV’s Power Tips for PHP (1)


In this article Justin provides some tips for improving our PHP applications such as a very simple but effective template technique.

Author Info:
By: Justin Vincent
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
January 29, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (1)
  2. · Never Breaking Out of PHP
  3. · Basic Template Theory
  4. · Refining the Previous
  5. · Accessing Other Variables from Within this Type of Template
  6. · Using Objects to Store Multiple Output Values
  7. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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”);
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials