Display Tutorials

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

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

    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



    Writing a Template System in PHP - Constructor and Output

    (Page 5 of 9 )

    I went back to write the class's constructor.

    function Page($template "template.html") {
      if (
    $this-&gt;page join(""file($template));
    "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.

    function output() {

    More Display Tutorials Articles
    More By bluephoenix

    blog comments powered by Disqus


    - Smart Auto Refresh
    - Writing a Template System in PHP

    Developer Shed Affiliates


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