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

本文介绍了PHP框架设计中关于页面模板的使用,通过多个引用示例和PHP代码片段,详细解析了如何在应用程序中集成和操作模板引擎,帮助初学者理解模板在PHP框架中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用:
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 r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值