ASP.NET 中母版页的 PHP 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1933168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Equivalent of Master page in ASP.NET
提问by Vijay
Is there any way to achieve the master page concept of ASP.NET in PHP?
有没有什么办法可以在PHP中实现ASP.NET的母版页概念?
采纳答案by Andrew Hare
You could use a template engine like Smarty:
你可以使用像Smarty这样的模板引擎:
Although Smarty is known as a "Template Engine", it would be more accurately described as a "Template/Presentation Framework." That is, it provides the programmer and template designer with a wealth of tools to automate tasks commonly dealt with at the presentation layer of an application.
尽管 Smarty 被称为“模板引擎”,但更准确地描述它是“模板/演示框架”。也就是说,它为程序员和模板设计者提供了丰富的工具来自动化通常在应用程序的表示层处理的任务。
回答by kowsheek
回答by troelskn
You can use auto_prepend_fileand auto_append_filetogether with output buffering.
您可以将auto_prepend_file和auto_append_file与输出缓冲一起使用。
回答by Nicolas Rodriguez
I made a simple proof-of-concept for a asp-like master pages in PHP. You can get it here: http://code.google.com/p/phpmasterpages/
我在 PHP 中为类似 asp 的母版页做了一个简单的概念验证。你可以在这里得到它:http: //code.google.com/p/phpmasterpages/
回答by Wookai
As other said, the way to go is to use a template engine. My preference goes to the Zend Framework, which is a bit more than just templates ;), but has a nice implementation of a layout with Zend_Layoutthat is very close to Master Pages in ASP.Net.
正如其他人所说,要走的路是使用模板引擎。我更喜欢Zend Framework,它不仅仅是模板 ;),但是Zend_Layout有一个很好的布局实现,非常接近 ASP.Net 中的母版页。
Check the quickstart guideto have an idea of how this works, and have a look at the full docto see the possibilities offered by the framework. Plus, using the framework gives you a nice MVC architecture and SEO-friends rewritten URLs...
查看快速入门指南以了解其工作原理,并查看完整文档以了解框架提供的可能性。另外,使用该框架为您提供了一个不错的 MVC 架构和 SEO 朋友重写的 URL...
回答by micahli123
Maybe just use include() since the variables in both file are in the same scope. Also ob_start() can record the output to the buffer so that the html codes can output later.
也许只使用 include() ,因为两个文件中的变量都在同一范围内。ob_start() 还可以将输出记录到缓冲区,以便稍后输出 html 代码。
回答by Raj Thakkar
The best and easy way to do is using include as other said. Create a header.php and footer.php and include them in every page you want to use. The approach is bit different compare to asp.net master pages but is as powerful as master pages
最好和最简单的方法是像其他人所说的那样使用包含。创建 header.php 和 footer.php 并将它们包含在您要使用的每个页面中。与 asp.net 母版页相比,该方法略有不同,但与母版页一样强大

