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->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->page; } ?>