HomePHP Page 5 - Processing File Data with Template Classes in PHP 5
Seeing the template pattern in action - PHP
If you're looking for a friendly guide on how to implement the template design pattern with PHP 5, then this group of articles might be what you need. Welcome to the final installment of the series that began with "Working with template classes in PHP 5." As the title indicates, this series steps you through creating template classes in PHP-controlled development environments, and also shows you how to use this rather uncommon pattern in real-world situations.
As I expressed in the section that you just read, below I included a short script which demonstrates in a friendly fashion the functionality of all the classes that I defined earlier. Also, the script in question assumes that there's a sample "default_file.txt" file that contains the following data:
This is line 1 of data file This is line 2 of data file This is line 3 of data file This is line 4 of data file This is line 5 of data file This is line 6 of data file This is line 7 of data file This is line 8 of data file This is line 9 of data file This is line 10 of data file
Having clarified that point, please examine the below example along with the different outputs that it generates:
try{ // create new 'FileProcessor' object $fileProc=new FileProcessor(); // create new 'UppercasedFileTemplate' object $upperFileTemplate=new UppercasedFileTemplate(); // display file contents as uppercased string // echo $upperFileTemplate->displayFormattedFileContents ($fileProc);
/* displays the following:
SIZE OF DATA FILE IS :280 BYTES CONTENTS OF DATA FILE ARE AS FOLLOWS : THIS IS LINE 1 OF DATA FILE THIS IS LINE 2 OF DATA FILE THIS IS LINE 3 OF DATA FILE THIS IS LINE 4 OF DATA FILE THIS IS LINE 5 OF DATA FILE THIS IS LINE 6 OF DATA FILE THIS IS LINE 7 OF DATA FILE THIS IS LINE 8 OF DATA FILE THIS IS LINE 9 OF DATA FILE THIS IS LINE 10 OF DATA FILE */
// create 'HTMLFileTemplate' object $htmlFileTemplate=new HTMLFileTemplate(); // display file contents as HTML //echo $htmlFileTemplate->displayFormattedFileContents ($fileProc);
/* displays the following (formatted as HTML)
Size of data file is: 280 bytes Contents of data file are as follows : This is line 1 of data file This is line 2 of data file This is line 3 of data file This is line 4 of data file This is line 5 of data file This is line 6 of data file This is line 7 of data file This is line 8 of data file This is line 9 of data file This is line 10 of data file */
// create 'XMLFileTemplate' object $xmlFileTemplate=new XMLFileTemplate(); // display file contents as XML header('Content-Type: text/xml'); echo $xmlFileTemplate->displayFormattedFileContents ($fileProc);
/* displays the following: (formatted as XML)
<contents> <size>Size of data file is: 280 bytes</size> <data>This is line 1 of data file</data> <data>This is line 2 of data file</data> <data>This is line 3 of data file</data> <data>This is line 4 of data file</data> <data>This is line 5 of data file</data> <data>This is line 6 of data file</data> <data>This is line 7 of data file</data> <data>This is line 8 of data file</data> <data>This is line 9 of data file</data> <data>This is line 10 of data file</data> </contents> */ } catch(Exception $e){ echo $e->getMessage(); exit(); }
As you can see, the above example demonstrates quite clearly how the template pattern works. All the template subclasses instantiated previously use the algorithm defined by the respective parent to display, in a specific format, data fetched from a specific file.
One last note before I finish this tutorial: as usual, feel free to tweak the source code of all the classes included here, so you can introduce improvements and acquire a better understanding on how the template pattern works.
Final thoughts
That's all for now. In this two-part series, hopefully you expanded your existing background on pattern-based programming with PHP, by learning a new one. Perhaps building template classes won't change your developer life forever, but it'll help you to have a more solid knowledge of how this pattern works.