简体   繁体   English

PHP页眉和页脚

[英]PHP headers and footers

I want to use php to create a consistent header and footer across my site using the php incluse tags. 我想使用php incluse标记在我的网站上创建一致的页眉和页脚。 When creating the header file, do I need the html and body tags or can I just start with the div id="header"....? 创建头文件时,我是否需要html和body标签,还是可以仅以div id =“ header” ....开头?

What you should worry about is the final outcome of the markup of the site, after you've included everything. 包含所有内容后,您应该担心的是网站标记的最终结果。

Example: 例:

header.php header.php

<div id="header"></div>

footer.php footer.php

<div id="footer"></div>

index.php index.php

<html>
<head></head>
<body>
    <?php include('header.php'); ?>
    <div id="content"></div>
    <?php include('footer.php'); ?>
</body>
</html>

为了获得正确的语义,请包含<html><body>标记,并确保在footer.php文件中将其关闭

You can have a section of HTML in a file like this. 您可以在这样的文件中包含一部分HTML。

<footer> This is my global footer! </footer>

Then in PHP you can use include() to include that html file. 然后在PHP中,您可以使用include()包含该html文件。 It will render the contents of the file to the output where you include it. 它将文件的内容呈现到包含它的输出中。

include 'globalFooter.html';

Do I need the html and body tags or can I just start with the div? 我需要html和body标签,还是可以仅从div开始?

No you do not. 你不可以。

Your question is not completely clear, but you seem to be asking whether you need any special tags in the included file. 您的问题尚不完全清楚,但是您似乎在询问是否需要在包含的文件中添加任何特殊标签。 The short answer is no—the included file is inserted into the including file verbatim, so it would contain whatever tags are required to render the header (or footer) you had in mind. 简短的答案是“否” —将包含的文件逐字插入到包含文件中,因此它将包含呈现所需的标头(或页脚)所需的任何标签。

Whatever you do, the result should be valid HTML. 无论您做什么,结果都应该是有效的HTML。

So if your index.php starts with <?php include "header.php" ?> then your header.php file should start with <!DOCTYPE html><html>... Same goes for the end. 因此,如果您的index.php<?php include "header.php" ?>开头,那么您的header.php文件应以<!DOCTYPE html><html>...开头。

Yes you should. 是的你应该。 It's a good idea, though, to use variables within the include to set such things as <title> ... 不过,最好在include中使用变量将诸如<title>类的内容设置为...

<?php 
$pagetitle="My page";
include('header.php');
?>
Content here
<?php include('footer.php'); ?>

where header.php is header.php在哪里

<html>
<head>
<title><?php echo $pagetitle; ?></title>
<!-- your meta tags etc -->
</head>
<body>

and footer is 和页脚是

<script>/* your javascript includes */</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM