php php中的母版页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8249220/
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
Masterpage in php
提问by something
I'm trying to incorporate something like Masterpages into a website, and have been wondering about something.
我正在尝试将 Masterpages 之类的内容合并到网站中,并且一直在想一些事情。
Should you have one template site where you require the header, footer and content(by passing a variable which points to a content file).
如果您有一个需要页眉、页脚和内容的模板站点(通过传递一个指向内容文件的变量)。
example of template.php:
template.php 示例:
require(header.php);
require($content.php);
require(footer.php);
or should you just require the header and footer template in every site.
或者您是否应该只需要每个站点中的页眉和页脚模板。
example of sonething_site.php:
sothing_site.php 示例:
require(header.php);
//content here
require(footer.php);
Thanks
谢谢
回答by Firnas
Simple Solution would be to have a file for master page and then calling it from your other pages, if you do not want to use any third-party template engine.
如果您不想使用任何第三方模板引擎,简单的解决方案是为母版页创建一个文件,然后从其他页面调用它。
Example: master.php
示例:master.php
<html>
<head>
<title>Title at Masterpage</title>
</head>
<body>
<div id="menu">
<a href="index.php">Home</a>
<a href="about.php">About Us</a>
<a href="contact.php">Contact Us</a>
</div>
<div><?php echo $content; ?></div>
<div id="footer">Footer to be repeated at all pages</div>
</body>
</html>
In your page for example about.php
在您的页面中,例如about.php
<?php
$content = 'This is about us page content';
include('master.php');
?>
NOTE:If you want to have page contents in a separate file rather than setting $content variable, you can include the file in your page and assign it to $content. Then in master.phpuse include($content)
instead of echo $content
注意:如果您希望将页面内容放在单独的文件中而不是设置 $content 变量,您可以将文件包含在您的页面中并将其分配给 $content。然后在master.php 中使用include($content)
而不是echo $content
回答by DarthVader
your first approach $content.php
doesnt seem very secure, i can insert my remote page there.
您的第一种方法$content.php
似乎不太安全,我可以在那里插入我的远程页面。
So your second approach is better, take a look at smarty. it s a great template engine.
所以你的第二种方法更好,看看smarty。它是一个很棒的模板引擎。
and be careful!!
小心!
回答by Raghavendra Devraj
First: Create a template page, I called mine master.php.
<body>
<div><?php include('menu.php');?></div>
<div><?php include($page_content);?></div>
<div><?php include('footer.php');?></div>
</body>
Second: Create a content piece, for example about_text.php.
<p>This is information about me.</p>
Third: Create the page with the actual name you want to use:
<?php
$page_content = 'about_text.php';
include('master.php');
?>
回答by regality
Usually it is better to go with option A. There is one template file and all of the pages only have content. The reason for this is that if you need to change the architecture of your site or the way that your template page behaves, you aren't screwed because you have to edit 1000 pages.
通常最好选择选项 A。有一个模板文件,所有页面都只有内容。这样做的原因是,如果您需要更改站点的架构或模板页面的行为方式,您不会被搞砸,因为您必须编辑 1000 个页面。
Unless you have a reason to do otherwise it is also not a bad idea to use a CMS.
除非您有理由这样做,否则使用 CMS 也不是一个坏主意。