设为首页 收藏本站
查看: 278|回复: 0

[经验分享] 转载 PHP Application Framework Design: 3 – Page Templates

[复制链接]

尚未签到

发表于 2017-4-7 11:35:44 | 显示全部楼层 |阅读模式
  转自: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 StartedPart 2: Managing Users,Part 3: Page Templates, Part 4: Forms and Events

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-361510-1-1.html 上篇帖子: java和php之间localhost的cookie共享 下篇帖子: 利用PHP获取网页的源代码或标题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表