【问题标题】:Masterpage in phpphp中的母版页
【发布时间】:2011-11-23 21:20:26
【问题描述】:

我正在尝试将 Masterpages 之类的内容整合到网站中,并且一直在想一些事情。

如果您有一个需要页眉、页脚和内容的模板站点(通过传递一个指向内容文件的变量)。

template.php 示例:

require(header.php);
require($content.php);
require(footer.php);

或者您应该只需要每个站点中的页眉和页脚模板。

sonething_site.php 示例:

require(header.php);
//content here
require(footer.php); 

谢谢

【问题讨论】:

  • 这么多问题:什么类型的网站?内容有多多样化?你看过 wordpress 是怎么做的吗(伟大的 php 编码)?
  • 你可能想看看smarty

标签: php master-pages


【解决方案1】:

如果您不想使用任何第三方模板引擎,简单的解决方案是为母版页创建一个文件,然后从其他页面调用它。

示例:ma​​ster.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>

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

<?php
   $content = 'This is about us page content';
   include('master.php');
?>

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

【讨论】:

    【解决方案2】:

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

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

    小心点!!

    【讨论】:

    • 你怎么能在那里插入你的远程网页?
    • @Nacereddine: Register globals,可能。
    • 听说过 MVC 吗?您不会将页面插入到模板中。看看 zend 框架控制器和 smarty 的使用。如果您不小心,任何人都可以在您的网站上插入页面。
    【解决方案3】:
    First: Create a template page, I called mine master.php.
    
    <body>
    <div><?php include('menu.php');?></div>
    <div><?php include($page_content);?></div>
    <div><?php include('footer.php');?></div>
    </body>
    
    Second: Create a content piece, for example about_text.php.
    <p>This is information about me.</p>
    
    Third: Create the page with the actual name you want to use:
    <?php
    $page_content = 'about_text.php';
    include('master.php');
    ?>
    

    【讨论】:

      【解决方案4】:

      通常最好选择选项A。模板文件只有一个,所有页面都只有内容。这样做的原因是,如果您需要更改网站的架构或模板页面的行为方式,您不会因为必须编辑 1000 个页面而搞砸。

      除非您有理由这样做,否则使用 CMS 也不错。

      【讨论】:

        【解决方案5】:

        您是否考虑过使用像 DwooSmarty 这样的 PHP 模板引擎。

        您可能喜欢的功能 Template Inheritance

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-17
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多