PHP框架设计入门之三:页面模板
来源:互联网 发布:linux命令执行返回值 编辑:程序博客网 时间:2024/06/07 00:39
引用:
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.
Templates
模板
引用:
Wouldn't it be nice if all the pages on your site had a similar style? Yes, it would… but that’s 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.
引用:
There are many template engines available for PHP but the approach we will use here is based on Brian Lozier’s 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.
引用:
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 presentation
Each page has a consistent feel and functionality
Simplified site maintenance
有限的额外开销(相对于完全不使用模板引擎而言)
业务逻辑层与表现层的分离
每个页面都有一致的体验和功能
简化站点维护
Inside the Belly of the Beast
深入核心
引用:
If you've 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 client's 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 don’t. 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代码:
<?=$Title?>
引用:
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 line
echo $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 doesn't 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代码:
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 files (and if they know PHP then even better). All in all, this gives us a pretty good design, if I do say so myself.
Summary
总结
引用:
At this point we have a fairly usable framework. However, we have not addressed several key goals: managing data in each page every time the page is submitted and displaying the data uniquely for each page using page templates. Both of these issues are resolved in Part 4 of this series using Forms.
- PHP框架设计入门之三:页面模板
- PHP框架设计入门之三:页面模板
- PHP框架设计入门之二:管理用户
- PHP框架设计入门之二:管理用户
- 设计模式入门之三
- PHP框架设计入门之一:布局篇
- PHP框架设计入门之一:布局篇
- 用PHP制作静态网站的模板框架(三)
- PHP框架中集成Smarty3模板引擎(三) ------ CodeIgniter 2.1
- BootStrap页面框架模板
- Spring框架快速入门之简介(三)
- 三大框架之struts入门1
- 三大框架之spring入门1
- Spark 入门之三:Spark运行框架
- php Laravel框架学习(三) 之 路由
- 设计模式入门-模板方法模式(php版)
- android应用框架设计之模板方法设计模式
- php网页模板设计
- C#遍历目录树的递归
- PHP框架设计入门之一:布局篇
- Asp.net生成htm静态文件的两种途径
- PHP框架设计入门之二:管理用户
- 强大的.NET反编译工具Reflector及插件
- PHP框架设计入门之三:页面模板
- 想要做好老板先要懂得怎样留住员工
- C#项目打包,并自动安装SQL数据库(VB.NET)
- C#窗口管理
- 在JavaScript中使用DOM技术动态控制表格
- Web.config文件详细说明
- 网通电信DNS地址
- 掌握 Ajax,第 1 部分: Ajax 入门简介 理解 Ajax 及其工作原理,构建网站的一种有效方法
- 总结几个产生上万流量的方法(SEO)