Blogger Information
Blog 33
fans 0
comment 2
visits 41949
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
网页模板的拆分和引入
hanyufeng的博客
Original
869 people have browsed it

运行效果:

L1.png

说明:

拆分header、footer部分为独立的php文件,再使用include或require 引入。

header、footer部分可以重复使用,节省时间,并保持了不同页面的风格统一,修改风格也很方便。

示例源码:

文档结构

L2.png

header.php

如果要在页面中引入不同的css外部样式表,可以把head标签的尾部放到页面中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>PHP学习博客</title>
    <link rel="stylesheet" href="static/css/style.css">
</head>

footer.php

<footer id="page_footer">
    <p>Copyright © 2017 PHP中文网</p>
    <nav>
        <ul>
            <li><a href="#">主页</a></li>
            <li><a href="#">关于我们</a></li>
            <li><a href="#">联系我们</a></li>
            <li><a href="#">合作</a></li>
        </ul>
    </nav>
</footer>
</body>
</html>

页面

<?php include 'inc\header.php' ?>
    <body>
    <header id="page_header">
        <h1>网页模板的使用(PHP方式)</h1>
    </header>
    <section id="posts">
        <article class="post">
            <header>
                <h2>摘要:include、require的使用及区别</h2>
                <p>发表时间
                    <time datetime="2017-12-30T23:39">2017-12-30 23:39</time>
                </p>
            </header>
            <aside>
                <p>
                    &ldquo;60天成就PHP大牛!&rdquo;
                </p>
            </aside>
            <p>
                include、require的区别
            </p>
            <p>
                include 的文件如果不存在,会在提示错误后继续运行后面的代码。
                require 的文件如果不存在,会在提示错误后停止运行。
            </p>

            <p>
                此外,还有include_once、require_once,用于避免重复引用。
            </p>
            <footer>
                <p><a href="comments"><i>25条回复</i></a> ...</p>
            </footer>
        </article>
    </section>

    <section id="sidebar">

        <nav>
            <h3>博文目录</h3>
            <ul>
                <li><a href="2017/10">2017年10月</a></li>
                <li><a href="2017/09">2017年10月</a></li>
                <li><a href="2017/08">2017年10月</a></li>
                <li><a href="2017/07">2017年10月</a></li>
                <li><a href="2017/06">2017年10月</a></li>
                <li><a href="2017/05">2017年10月</a></li>
                <li><a href="2017/04">2017年10月</a></li>
                <li><a href="2017/03">2017年10月</a></li>
                <li><a href="2017/02">2017年10月</a></li>
                <li><a href="2017/01">2017年10月</a></li>
                <li><a href="all">更多</a></li>
            </ul>

        </nav>

    </section>

<?php include 'inc\footer.php' ?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!