PHP 相当于 site.master?
ASP.Net 的优点之一是能够设置一个 site.master
文件来保存站点中所有重复的 HTML/代码,并且仍然能够由个人更改其中的内容网站页面。 (例如,更改每个页面的
过去在 PHP 中,我已经使用服务器端包含来删除重复的 HTML/代码(例如文档顶部、主导航、页脚等),但显然您无法更改页面中的任何内容。
有没有办法在 PHP 中实现 site.master
类型功能? 如果没有,有什么好方法可以删除重复的 HTML/代码,同时仍然能够更改页面标题、突出显示的导航等内容。
谢谢。
One of the nice things about ASP.Net is the ability to set a site.master
file that holds all the repeated HTML/code from a site, and still be able to alter things on it from an individual website page. (For example, changing the <title>
tag for every page, or adding other things to the <head>
of your document.)
In the past in PHP I've used Server Side Includes to remove duplicate HTML/code (such as the top of the document, main navigation, footer, etc.) but obviously you can't alter any of the contents from the page.
Is there any way to implement site.master
type functionality in PHP? If not, what's a good way to remove repeated HTML/code while still being able to change things like the page's title, highlighted navigation, etc.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是所谓的模板引擎,并且有大量的 php 引擎。
聪明人就是其中之一
This is just called Template Engine and there is tons of engine for php.
Smarty is one of them
引入公共片段的最简单方法是使用 require() ,它允许您在当前文件中包含其他文件。
如何使用它的示例:
header.php - 可重用片段
pageContents.php
The simplest way of pulling in common fragments is to use
require()
which will allow you to include other files within the current file.An example of how you might use it:
header.php - a reusable fragment
pageContents.php
您正在寻找的是为 PHP 构建的模板引擎。 ASP 是一个内置模板的框架,它为您提供您所引用的功能(即模板、成员资格提供程序等)。
为了在 PHP 中获得相同的功能,您将需要使用框架或模板引擎。
一些示例:
What you are looking for is a templating engine built for PHP. ASP is a framework which has templating built in which provides you the functionality that you are referring to (i..e templates, membership provider, etc.)
In order to get the same in PHP you will need to use either a framework or a templating engine.
Some samples:
PHP 中没有这样的功能,您可以将组件移动到单独的文件中,并使用
include
或require
函数将它们包含在主文件中。不过,某些框架确实提供(模拟)此类功能。例如,在 cakePHP 中,您可以拥有一个布局文件,您可以在其中输出页面。
there is no such functionality in PHP, you can move your components into separate files and include them in your main file using
include
orrequire
functions.Some frameworks do provide (simulate) such functionality though. e.g. In cakePHP you can have a layout file where you can output your pages.