转载 PHP Application Framework Design: 3 – Page Templates
转自:http://www.lbsharp.com/wordpress/index.php/2005/10/13/php-application-framework-design-3-page-templates/This is part 3of a multi-part series on the design of a complete application frameworkwritten in PHP. In part 1, we covered the basic class structure of theframework and laid out the scope of the project. The second part describedmethods for managing users and session data. This part describes a practicalimplementation of page templates and the separation of application logic fromthe presentation layer.
Templates
Wouldn’t it benice if all the pages on your site had a similar style? Yes, it would… butthat’s not the only reason for using page templates. In fact, if thats all thatyou require, style sheets should solve the problem with much less overhead. Ifyou are looking to add a simple header of footer to every page, Apache providesthat functionality via server side includes (or you can use PHP to simply readin an file and output it to the top of each page). In the context of thisframework, however, what we are trying to accomplish is a bit moresophisticated. In effect, templates allow us to add a separate presentationlayer to our web application. This approach is similar (though much simpler) tothe one employed in ASP.NET.
There are manytemplate engines available for PHP but the approach we will use here is basedon Brian Lozier’s article Beyond The Template Engine.The idea is that most of the existing template engines provide much moreoverhead that we want for what we need to accomplish. In fact, PHP can do whatwe need in just a few lines of code which open up a text file and replace allthe place-holders with dynamic content. So if we encapsulate that functionalityin a class and put a cherry on top we end up with class_template.php and a realization of a presentation layer in our application framework.
In short, we willachieve the following benefits from implementing templates as described below:
[*]Limited overhead (compared to not using templatesat all)
[*]Separation of the business logic layer from thepresentation
[*]Each page has a consistent feel and functionality
[*]Simplified site maintenance
Inside the Belly ofthe Beast
If you’ve beenfollowing this series from Part 1 you will notice that so far we have described a page as an object of aclass which performs some operations but does not output anything to the screen(unless you decided to use your own poetic license to interprete the action ofoutputting to the client’s browser as just another operation). The reason forthis is that all aspects regarding the way the results is displayed are handledby the template engine and stored in the page template files. Each page willuse the same template file (preferably) and embed the the dynamic content usingthe interface provided by the template class. Actually, in the final version ofthe application framework, each page reads in a generic template for the pagelayout and then nests another page template as a form. The form templates areunique to each page and allow for greater flexibility. For now, lets just keepthings simple. Read Part 4 of the series to see how the forms are implemented.
Page templates, asimplemented in this framework, are nothing more that PHP files. In fact, youcan place any valid PHP command into these files but we will employ the honorprinciple to ensure that you don’t. After all, the goal was to separate thepresentation layer from the application logic and placing code into thesetemplate files defeats that purpose. To embed the dynamic content into thetemplates simply place a PHP variable into the template file like so:
<?=$Title?>.
Then, inside the class for this page, we can set the variable as follows:
$this->page->set("Title","PageTitle");.
Everything else is taken care of by the framework. All you have to do isspecify the right template file to use and do write the line
echo$this->page->fetch($this->template);
when you are ready to output the result to the screen. (And even that part istaken care of for you in the base class.)
If using thetemplate engine doesn’t seem too difficult, look how easy it is to implementthe engine itself. Since we are using PHP files as templates, the PHP parserwill take care of all the hard work. All we need to do is maintain an array ofvalues that we want to assign to the place-holders (i.e. when you use the set()method). Then we need to implement a fetch() method which will extract the values used for the place-holders into thelocal namespace, read the template file into an output buffer, and then returnthe results. Here is the code in all its glory:
functionfetch($file) {
extract($this->vars); // Extract the vars to localnamespace
ob_start(); // Start output buffering
include($this->path . $file); // Include the file
$contents = ob_get_contents(); // Get thecontents of the buffer
ob_end_clean(); // End buffering and discard
return $contents; // Return the contents
}
This approach hasseveral advantages compared to other template engines which implement their ownparser to parse the page templates. First of all, we have all the power andspeed of PHP at our disposal. If occasionally we have to sneak a little logicinto the template files then we have the option to do so. Furthermore, thetemplates execute as fast as PHP itself so we are not adding much overhead tothe generation of each page. Template engines that have their own parsersimplemented in PHP are slower and those that are implemented in C requireinstalling extra modules on the web server. Finally, the users of the templateengine (i.e. the page designers) do not need to learn a new syntax to createthe files (and if they know PHP then even better). All in all, this gives us apretty good design, if I do say so myself.
Summary
At this point wehave a fairly usable framework. However, we have not addressed several keygoals: managing data in each page every time the page is submitted anddisplaying the data uniquely for each page using page templates. Both of theseissues are resolved in Part 4 of this series using Forms.
Navigate: Part 1: Getting Started, Part 2: Managing Users,Part 3: Page Templates, Part 4: Forms and Events
页:
[1]