gpt4 book ai didi

php - php 中的母版页

转载 作者:可可西里 更新时间:2023-11-01 00:06:38 24 4
gpt4 key购买 nike

我正在尝试将 Masterpages 之类的东西合并到一个网站中,并且一直在想一些事情。

如果您有一个模板网站,您需要页眉、页脚和内容(通过传递一个指向内容文件的变量)。

template.php 示例:

require(header.php);
require($content.php);
require(footer.php);

或者您是否应该在每个站点中只需要页眉和页脚模板。

sonething_site.php 示例:

require(header.php);
//content here
require(footer.php);

谢谢

最佳答案

如果您不想使用任何第三方模板引擎,简单的解决方案是为母版页创建一个文件,然后从您的其他页面调用它。

例子:ma​​ster.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>

例如在您的页面中 about.php

<?php
$content = 'This is about us page content';
include('master.php');
?>

注意:如果您想将页面内容放在一个单独的文件中而不是设置 $content 变量,您可以将该文件包含在您的页面中并将其分配给 $content。然后在 master.php 中使用 include($content) 而不是 echo $content

关于php - php 中的母版页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8249220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com