我正在开发我的第一个php网站,我遇到了一个我无法弄清楚的问题.我正在尝试拥有一个包含我的结构的php页面,以及其他将html注入其中的页面,同时保留url更改,因此我仍然可以直接链接页面.
到目前为止,这就是我正在做的事情,但它看起来效率不高:
的index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
<?php include("home.php"); ?>
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
about.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
About me!
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这感觉完全错了,如果我想改变我的容器类,或改变结构,我现在必须在两个地方而不是一个地方做.
在ASP.net MVC中,我将有一个包含我的HTML结构的Layout_Head.cshtml文件,在里面我可以从不同的页面渲染视图,url会更改但是布局总是首先呈现,然后控制器/操作负责注入所需视图的html.
我如何在PHP中复制它?
通常人们使用 php 包含更像这样的模板:
头文件
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
Run Code Online (Sandbox Code Playgroud)
页脚.php
</div> <!-- .container -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
关于.php
<?php include('header.php'); ?>
... content goes here ...
<?php include('footer.php'); ?>
Run Code Online (Sandbox Code Playgroud)
这样您就不需要在您制作的每个模板上不断重复开始/结束标签。