php框架设计入门,PHP框架设计入门之三:页面模板

本文是PHP框架设计系列的第三部分,介绍了如何实现页面模板以实现应用逻辑与展示层的分离。通过使用简单的PHP文件作为模板,减少了额外的开销,并允许在模板中插入动态内容。通过设置变量并使用fetch()方法,可以轻松管理和输出页面内容,简化了站点维护。模板引擎的设计使得即使在模板文件中也可以插入少量逻辑,同时保持高效和灵活性。
摘要由CSDN通过智能技术生成

转载自:

PHP框架设计入门之三:页面模板

引用:

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

模板

引用:

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.

如果你网站上的所有页面都有一个相似的外观,这难道不是一件美妙的事情吗?是,确实是很美妙,但是那并不是使用页面模板的唯一原因。实际上,如果你的要求

此限于此(译注:即页面有一个相似的外观),那么用样式表(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 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.

虽然有许多的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 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代码,我们只能与你做"君子约

定"(honor

principle)来确保你不会这么做。毕竟,我们的目标是将表现层从应用逻辑中分离出来,而将代码放到这些模板文件中则违背了我们的初衷。要将动态内

容插入到模板中,只需要简单地像下面这样把一个PHP变量放进去:

复制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解析器会处理所有费事的工作。我们所要做

的,只是维护一个要赋给占位符(比如:当你使用set()的时候)的数组的值。我们还必须实现一个fetch()方法,这个方法会提取占位符所要用到的变

量,把它们放入到局部的命名空间中(译注:就是使那些变量在局部的作用域中有效),然后读取模板文件的内容到输出缓冲区中,再返回相应的结果。下面是实现

的代码:

复制PHP内容到剪贴板

PHP代码:

functionfetch($file) {extract($this->vars);// Extract the vars to local namespaceob_start();// Start output bufferinginclude($this->path.$file);// Include the file$contents=ob_get_contents();// Get the contents of the bufferob_end_clean();// End buffering and discardreturn$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.

相比于其他实现了他们自己的解析器来解析页面的模板引擎,这种方法有几个优点。首先,我们充分利用了PHP的功能强和速度快。如果有时候,我们不得不偷偷

地将一些逻辑放到模板文件中,我们也可以做到。另外,模板的执行速度就跟PHP自身一样快,所以,生成每一个页面并没有给我们带来过多的额外开销。用

PHP实现了自己的解析机制的模板引擎,运行起来要慢得多,而那些用C实现了的,都要求在Web服务器上安装扩展模块。最后,模板引擎的使用者(比如:页

面设计师)不需要为了创建页面而学习一种新的语法(当然,他们如果懂PHP更好)。总而言之,如果要我说的话,这会给我们带来优美的设计。

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.

现在,我们已经有了一个相当好用的框架。然而,我们还没有考虑几个关键问题:管理每个页面提交的数据,使用页面模板为每个页面独特地显示数据。这两个方面的问题都将在本系列教程的第四部分(使用表单)解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值