php中的主页

som*_*ing 3 php master-pages

我正在尝试将类似Masterpages的内容整合到一个网站中,并一直在想些什么.

您是否应该有一个模板站点,您需要页眉,页脚和内容(通过传递指向内容文件的变量).

template.php的示例:

require(header.php);
require($content.php);
require(footer.php);
Run Code Online (Sandbox Code Playgroud)

或者您是否只需要在每个站点中使用页眉和页脚模板.

sonething_site.php的示例:

require(header.php);
//content here
require(footer.php); 
Run Code Online (Sandbox Code Playgroud)

谢谢

Fir*_*nas 7

如果您不想使用任何第三方模板引擎,那么简单的解决方案是拥有母版页的文件,然后从其他页面调用它.

示例:master.php

<html>
   <head>
      <title>Title at Masterpage</title>
   </head>

   <body>

      <div id="menu">
         <a href="index.php">Home</a>
         <a href="about.php">About Us</a>
         <a href="contact.php">Contact Us</a>
      </div>

      <div><?php echo $content; ?></div>

      <div id="footer">Footer to be repeated at all pages</div>

   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在您的页面中,例如about.php

<?php
   $content = 'This is about us page content';
   include('master.php');
?>
Run Code Online (Sandbox Code Playgroud)

注意:如果要将页面内容放在单独的文件中而不是设置$ content变量,则可以在页面中包含该文件并将其分配给$ content.然后在master.php中使用include($content)而不是echo $content


Dar*_*der 3

您的第一种方法$content.php似乎不太安全,我可以在那里插入我的远程页面。

所以你的第二种方法更好,看看 smarty。它是一个很棒的模板引擎。

小心!