我正在开发我的第一个 php 网站,我遇到了一个我无法解决的问题。我试图拥有一个包含我的结构的 php 页面,以及其他在其中注入 html 的页面,同时保留 url 更改,以便我仍然可以直接链接页面。
到目前为止,这就是我正在做的事情,但似乎效率不高:
索引.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>
关于.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>
这感觉完全错误,如果我想更改容器类或更改结构,我现在必须在两个地方而不是一个地方进行操作。
在 ASP.net MVC 中,我将有一个 Layout_Head.cshtml 文件,其中包含我的 HTML 结构,在内部我可以渲染来自不同页面的视图,url 发生变化,但布局始终首先渲染,然后控制器/操作负责注入所需视图的 html。
我如何在 PHP 中复制它?
通常人们使用 php include 来制作模板,如下所示:
header.php
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
页脚.php
</div> <!-- .container -->
</body>
</html>
关于.php
<?php include('header.php'); ?>
... content goes here ...
<?php include('footer.php'); ?>
这样您就不需要在制作的每个模板上不断重复开始/结束标签。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)