2014-02-16 21 views
5

我正在处理我的第一个php网站,我遇到了一个我看不出来的问题。我试图让一个包含我的结构的php页面,以及其他一些注入其html内容的php页面,同时保留url更改,以便我仍然可以直接链接页面。加载php文件到布局模板?

到目前为止,这是我在做什么,但它似乎没有有效的:

的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> 

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> 

这感觉完全错误,如果我想改变我的容器类,或改变结构,我现在必须在两个地方做而不是一个。

在ASP.net MVC中,我将有一个Layout_Head.cshtml文件,它将包含我的HTML结构,并且在里面我可以渲染来自不同页面的视图,url更改,但布局总是先渲染,然后控制器/注意所需视图的html注入。

我该如何在PHP中进行复制?

+0

为什么不把'body'和'#container'标签放在'head.php'文件中?您无法将ASP.net MVC与PHP进行比较。 PHP是一种语言,ASP.NET MVC是一个框架。如果你想要MVC,可以使用像Codeigniter这样的框架。 –

+0

我明白这是不一样的,我只是试图解释我通常如何做:)为了将body和#container放在head.php中,当我加载about.php时,我如何告诉它加载#container中的数据? – LanFeusT

+0

你不要告诉它加载任何东西; PHP包括简单的回显数据,就好像你已经把代码放在那里一样。所以你的头文件包含起始HTML和任何其他包装,并且页脚文件包含结尾体/ html标签。 –

回答

4

通常情况下人们使用PHP包括模板更是这样的:

的header.php

<html> 
    <head> 
    <title></title> 
    </head> 
    <body> 
    <div class="container"> 

footer.php

</div> <!-- .container --> 
    </body> 
</html> 

about.php

<?php include('header.php'); ?> 
    ... content goes here ... 
<?php include('footer.php'); ?> 

这样您就不需要在每个制作的模板上不断重复开始/结束标记。

+0

噢,我看到了...我试图只有一个包含开始和结束标记(页眉和页脚合并成一个)的文件,然后将数据注入......现在更有意义!谢谢! – LanFeusT

+0

@LanFeusT是的布局更合理,但PHP包括只是不支持这种行为。当我开始在Rails中使用布局时,我发现它比PHP包含的功能强大得多。许多PHP框架(例如Wordpress)仍然使用包括而不是布局。 –

+0

我明白了,谢谢!这对我来说现在工作得很好! – LanFeusT