Display Tutorials

  Home arrow Display Tutorials arrow Page 5 - Writing a Template System in PHP
DISPLAY TUTORIALS

Writing a Template System in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 69
    2004-01-13

    Table of Contents:
  • Writing a Template System in PHP
  • The Implementation
  • The Template File
  • The Class Skeleton
  • Constructor and Output
  • Replacing Tags
  • Including Dynamic Files
  • The Final Code
  • Tricks

  •  
     

    SEARCH CODEWALKERS

    Writing a Template System in PHP - Constructor and Output


    (Page 5 of 9 )

    I went back to write the class's constructor.

    <?php
    function Page($template "template.html") {
      if (
    file_exists($template))
        
    $this-&gt;page join(""file($template));
      else
        die(
    "Template file $template not found.");
    }
    ?>

    I like to allow for default arguments to the methods I write when it makes sense to do so. In this case the default template file is template.html, but it can always be overridden by simply passing the name of another file to the constructor.

    I needed to first check to see if the template file existed. If it did then I could load it into memory. If it didn't then there was nothing I could do so I killed the script and sent a nice error message to the user.

    Then I turned my attention to the output method, the code of which should be pretty self-explanatory.

    <?php
    function output() {
      echo 
    $this-&gt;page;
    }
    ?>

    More Display Tutorials Articles
    More By bluephoenix

    blog comments powered by Disqus

    DISPLAY TUTORIALS ARTICLES

    - Smart Auto Refresh
    - Writing a Template System in PHP

    Developer Shed Affiliates

     



    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap