




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
引用:This is part 3 of a multi-part series on the design of a complete application framework written in PHP. In part 1, we covered the basic class structure of the framework and laid out the scope of the project. The second part described methods for managing users and session data. This part describes a practical implementation of page templates and the separation of application logic from the presentation layer.这是PHP框架设计入门系列教程的第三部分。在第一部分,我们已经介绍框架的基础类结构,并展示了项目的大体。第二部分叙述了管理用户和会话数据的一些方法。这一部分,我们谈论页面模板的具体实现及应用逻辑与表现层的分离。Templates模板 引用:Wouldnt it be nice if all the pages on your site had a similar style? Yes, it would but thats not the only reason for using page templates. In fact, if thats all that you require, style sheets should solve the problem with much less overhead. If you are looking to add a simple header of footer to every page, Apache provides that functionality via server side includes (or you can use PHP to simply read in an file and output it to the top of each page). In the context of this framework, however, what we are trying to accomplish is a bit more sophisticated. In effect, templates allow us to add a separate presentation layer to our web application. This approach is similar (though much simpler) to the one employed in ASP.NET.如果你网站上的 所有页面都有一个相似的外观,这难道不是一件美妙的事情吗?是,确实是很美妙,但是那并不是使用页面模板的唯一原因。实际上,如果你的要求此限于此(译 注:即页面有一个相似的外观),那么用样式表(CSS)就足够解决问题,并且可以使你少花费许多精力。如果你在寻找一种给每个页面加入一个简单的页头、页 脚的方法,可以使用Apache提供的功能,那些功能通过服务端包含实现(或者你可以使用PHP简单地读入一个文件,然后输出文件的内容到每一个页面的顶 部)。然而,在这个框架的中,我们所要尝试完成的事情要更复杂一些。从效果上来看,模板可以为我们的web应用程序加入一个分离了的表现层。这种方法与 ASP.NET中实现的类似,但相比起来简单许多。 引用:There are many template engines available for PHP but the approach we will use here is based on Brian Loziers article Beyond The Template Engine. The idea is that most of the existing template engines provide much more overhead that we want for what we need to accomplish. In fact, PHP can do what we need in just a few lines of code which open up a text file and replace all the place-holders with dynamic content. So if we encapsulate that functionality in 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.虽然有许多的PHP模板引擎可供我们选择,但我们这里还是以Brian Lozier的文章Beyond The Template Engine中谈论的模板引擎为基础。这样做的理由是,就我们需要完成的事情来说,许多现有的模板引擎都过于笨重,要有不少额外的开销。实际上,像打开文 本文件并将所有的占位符替换成动态内容这些我们需要做的事情,只需要几行PHP代码就可以完成。所以,如果我们将那些功能都封装到一个类中,将一切都写进 class_template.php,那真可以称得上是画龙点睛,这样子,我们的应用程序框架中就有了一个表现层的实现。 引用:In short, we will achieve the following benefits from implementing templates as described below:Limited overhead (compared to not using templates at all)Separation of the business logic layer from the presentationEach page has a consistent feel and functionalitySimplified site maintenance简而言之,我们要实现的模板引擎要有以下的几个优点:有限的额外开销(相对于完全不使用模板引擎而言)业务逻辑层与表现层的分离每个页面都有一致的体验和功能简化站点维护Inside the Belly of the Beast深入核心 引用:If youve been following this series from Part 1 you will notice that so far we have described a page as an object of a class 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 of outputting to the clients browser as just another operation). The reason for this is that all aspects regarding the way the results is displayed are handled by the template engine and stored in the page template files. Each page will use the same template file (preferably) and embed the the dynamic content using the interface provided by the template class. Actually, in the final version of the application framework, each page reads in a generic template for the page layout and then nests another page template as a form. The form templates are unique to each page and allow for greater flexibility. For now, lets just keep things simple. Read Part 4 of the series to see how the forms are implemented. 如果你从第一部分一直看到这里,你会注意到,到现在为止,我们已经将页面描述成一个类中的一个对象,这个对象 实现了一些操作,但却没有输出任何东西到屏幕上(除非你别出心裁地将输出结果(到客户端浏览器)的行为理解为另外的操作)。其中的原因是,结果显示方式的 方方面面都由模板引擎处理,并且存储在页面模板文件中。每个页面(最好)都使用相同的模板文件,并通过模板类提供的接口插入动态的内容。其实,在我们最终 版本的程序框架中,会有一个通用的模板来呈现我们每个页面的外观,然后往其中嵌入另外一个页面模板作为一个表单。表单模板对每个页面来说是唯一的,并且有 比较强的灵活性。现在,我们一切从简,你可以阅读本系列教程的第四部分,了解表单是如何实现的。 引用:Page templates, as implemented in this framework, are nothing more that PHP files. In fact, you can place any valid PHP command into these files but we will employ the honor principle to ensure that you dont. After all, the goal was to separate the presentation layer from the application logic and placing code into these template files defeats that purpose. To embed the dynamic content into the templates simply place a PHP variable into the template file like so:在这个框架中,已经实现了页面模板,说起来,页面模板也不过是PHP文件。实际上,你可以 在这些文件中放入任何有效的PHP代码,我们只能与你做君子约定(honor principle)来确保你不会这么做。毕竟,我们的目标是将表现层从应用逻辑中分离出来,而将代码放到这些模板文件中则违背了我们的初衷。要将动态内 容插入到模板中,只需要简单地像下面这样把一个PHP变量放进去: 复制PHP内容到剪贴板 PHP代码:引用:Then, inside the class for this page, we can set the variable as follows:然后,在这个页面的类当中,我们可以使用下面的语句来设置变量的值: 复制PHP内容到剪贴板 PHP代码:$this-page-set(Title,Page Title);. 引用:Everything else is taken care of by the framework. All you have to do is specify the right template file to use and do write the lineecho $this-page-fetch($this-template);when you are ready to output the result to the screen. (And even that part is taken care of for you in the base class.)其余的事情框架都会处理好。你所要做的事情,仅仅是指定要使用的模板文件,然后当你准备把结果输出到屏幕上的时候,再写上echo $this-page-fetch($this-template);这样的一行就可以了(甚至那一部分,基类都为你处理好了)。 引用:If using the template engine doesnt seem too difficult, look how easy it is to implement the engine itself. Since we are using PHP files as templates, the PHP parser will take care of all the hard work. All we need to do is maintain an array of values 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 the local namespace, read the template file into an output buffer, and then return the results. Here is the code in all its glory:如果使用模板引擎看起来并不太难,你可以看一看实现引擎本身有多容易。由于我们使用了PHP文件作为模板,PHP解析器会处理所有费 事的工作。我们所要做的,只是维护一个要赋给占位符(比如:当你使用set()的时候)的数组的值。我们还必须实现一个fetch()方法,这个方法会提 取占位符所要用到的变量,把它们放入到局部的命名空间中(译注:就是使那些变量在局部的作用域中有效),然后读取模板文件的内容到输出缓冲区中,再返回相 应的结果。下面是实现的代码: 复制PHP内容到剪贴板 PHP代码:function fetch($file) extract($this-vars); / Extract the vars to local namespace ob_start(); / Start output buffering include($this-path . $file); / Include the file $contents = ob_get_contents(); / Get the contents of the buffer ob_end_clean(); / End buffering and discard return $contents; / Return the contents引用:This approach has several advantages compared to other template engines which implement their own parser to parse the page templates. First of all, we have all the power and speed of PHP at our disposal. If occasionally we have to sneak a little logic into the template files then we have the option to do so. Furthermore, the templates execute as fast as PHP itself so we are not adding much overhead to the generation of each page. Template engines that have their own parsers implemented in PHP are slower and those that are implemented in C require installing extra modules on the web server. Finally, the users of the template engine (i.e. the page designers) do not need to learn a new syntax to create the fil
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 传媒公司扫描仪维护考核规定
- 传媒公司客户合作评估标准办法
- 传媒公司客户合作评估记录办法
- 光学显微镜在煤矿矿井水害预测中的应用考核试卷
- 河北高考的数学试卷
- 共享经济模式分析考核试卷
- 半导体器件模型在传感器设计中的应用考核试卷
- 会展旅游合同中的知识产权保护问题考核试卷
- 建邺2024初三二模数学试卷
- 2025年细分医疗领域类:医疗行业数字化转型与行业竞争力提升研究报告
- 转让律所合伙人份额协议书范文
- 《骆驼祥子》名著阅读课件
- 2023医疗质量安全核心制度要点释义(第二版)对比版
- 商品房预售资金监管实施方案
- 居间服务合同范本复制版
- 游戏开发外包合同
- 2024-2030年中国公路养护行业市场深度调研及投资策略与投资前景研究报告
- 虚拟货币行业市场深度分析报告
- 施工现场的防汛应急预案
- 《学科教学与德育及心理健康教育有机融合的研究》课题结题报告
- 四年级数学下册计算题(每日一练33份)
评论
0/150
提交评论