1.我的header模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="dist/css/bootstrap.css"> <script src="jquery-3.2.1.js"></script> <script src="dist/js/bootstrap.js"></script> <style> footer{ line-height: 50px; } </style> <title>YourTitle</title> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <nav class="navbar navbar-default navbar-inverse"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Serendipity-Ling</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li> <li><a href="#">路线</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">分类<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> <li role="separator" class="divider"></li> <li><a href="#">One more separated link</a></li> </ul> </li> </ul> <form class="navbar-form navbar-left"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">搜索</button> </form> <ul class="nav navbar-nav navbar-right"> <!--<li><a href="#">联系我</a></li>--> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">关于 <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">QQ</a></li> <li><a href="#">We Chat</a></li> <li><a href="#">Mail</a></li> <!--<li role="separator"></li>--> <!--<li><a href="#">Separated link</a></li>--> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </div> </div> <!-- header-->
2. 我的footer模板:
<div class="row"><!-- footer--> <div class="col-md-12"> <footer class="navbar navbar-default navbar-inverse text-center"> <span><a href="#">凌真华的博客</a></span> </footer> </div> </div> </div> </body> </html>
3. 当两份模板做好后,采用php里的include函数
首先我先建立一个公共文件夹,文件夹名为inc。讲上面的两个模板各自保存为php文件,取名header.php和footer.php, 放在inc里。
采用下面的代码,将这两个模板放在同一页面
<?php include_once 'inc/header.php' ?> <!-- 页面主体内容--> <div height="200px" style="background-color: skyblue;border: 1px solid black;margin-bottom: 20px"> <h2>大家好,我是页面主体内容</h2></div> <?php include_once 'inc/footer.php' ?>
include与require的区别:
require一个文件存在错误的话,那么程序就会中断执行了,并显示致命错误
include一个文件存在错误的话,那么程序不会中端,而是继续执行,并显示一个警告错误。
include_once:表示只将文件导入一次
include一般是放在流程控制的处理部分中PHP程序网页在读到include的文件时,才将它读进来。这种方式可以把程序执行时的流程简单化。