【发布时间】:2013-09-22 07:59:48
【问题描述】:
我正在努力让我的生活更轻松,并让所有页面在一个文件中具有相同的页脚和标题内容,这就是我目前所拥有的:
包含内容的页面
<?php
include ("content.php");
echo $page_header;
?>
<div id="content">
</div>
<?php
echo $page_footer;
?>
content.php
<?php
// This is the header which we want to have on all pages
$page_header = include ("resources/content/header.php");
// This is the footer which we want on all pages
$page_footer = include ("resources/content/footer.php");
?>
Header.php 示例
<html>
<head>
<title>This is my title</title>
</head>
<body>
<div id="logo">
</div>
Footer.php 示例
<div id="footer">Copyright to me!</div>
</body>
</html>
我遇到的问题是我的 header.php 内容并未全部显示并导致页面格式出现问题。 header.php 确实包含一些 php if 语句和一些内联 javascript... 这应该有关系吗?
有没有更好的方法?
请注意:我在本地使用 PHP 5,而我的服务器是 PHP 4,所以答案需要对两者都有效
【问题讨论】:
-
include ("resources/content/footer.php");而不是将其分配给变量 -
这种技术真的没有什么问题,你试过验证你的 HTML 吗?没有任何理由某些条件 php 或内联 JS 应该有所作为,您可以发布 header.php 内容
-
我在类似的问题here 中写过这个。
-
@mdesdev 甚至与我的问题无关。
-
好的,但据我所知,您正在尝试创建母版页、相同的页眉、页脚等。内容多变,我的解决方案属于同一类别,除非您尝试不同的方法。