如何在一行中添加php中的标准元素

时间:2012-11-30 10:34:54

标签: php html

我创建了一些php文件以下是他们的名字:

  1. standard_head.php(包含基本标准html代码)
  2. 的header.php
  3. navbar.php
  4. 的sidebar.php
  5. footer.php
  6. standard_footer.php(包含关闭html标签)
  7. 页面内容在每个页面中都会有所不同,还会包含html内容。

    我正在尝试创建大量的php页面,它们将直接使用上述所有页面。

    现在我可以直接使用include语句为每一个使用它们,但我想知道是否可以将所有这些包含在一起只是一个声明?

    <?php include('../includes/standard_head.php'); ?>
            <?php include('includes/header.php'); ?>
                <div id="wrapper">
                    <?php include('includes/nav.php'); ?>
                    <div id="content">
                    <!-- my page content -->
                    </div> <!-- end #content -->
                    <?php include('includes/sidebar.php'); ?>
                </div> <!-- End #wrapper -->
            <?php include('includes/footer.php'); ?>
    <?php include('../includes/standard_footer.php'); ?>
    

5 个答案:

答案 0 :(得分:1)

你可以这样(请注意,这是一个非常基本的例子,我不会在不考虑元标记,页面标题等的情况下自己使用它。)

<强>的template.php

<?php include('../includes/standard_head.php'); ?>

<?php include('includes/header.php'); ?>

    <div id="wrapper">

        <?php include('includes/nav.php'); ?>

            <div id="content">
                <?php echo $content; ?>
            <!-- my page content -->
            </div> <!-- end #content -->

        <?php include('includes/sidebar.php'); ?>

    </div> <!-- End #wrapper -->

    <?php include('includes/footer.php'); ?>

<?php include('../includes/standard_footer.php'); ?>

然后,在页面上(例如index.php):

<强>的index.php

<?php
$content = '<h1>Welcome</h1>';
include 'template.php';
?>

P.S:如果沿着这条路走下去,请务必先查看Output Control Functions

答案 1 :(得分:0)

只需创建一个包含它们列表的PHP文件,即

<?php include("this file"); 
      include("the other file");
?>

然后只需添加该文件。

答案 2 :(得分:0)

不是你的问题的答案,但你可以使用像Twig这样的模板引擎。 http://twig.sensiolabs.org/

答案 3 :(得分:0)

尝试使用更复杂的模板系统,如Smarty或某些MVC框架,如Zend(这不是必需的,但可以让您更轻松地创建复杂的源代码),然后构建如下脚本:

<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>

    <div id="wrapper">

        <?php include('includes/nav.php'); ?>
            <div id="content">
                <?php echo $menu->getCustomEntries(); ?>
            </div>

$menu将是您的自定义对象,其中包含显示菜单和子菜单的方法......

答案 4 :(得分:-1)

除了一个参数之外,没有直接的方法可以包含多个文件,包括include / require函数。虽然你可以使用以下逻辑。

`#include_all_files.php 包括( '../包括/ standard_head.php'); 包括( '包括/ header.php文件'); ...

在其他文件中使用上述文件

包括( '包括/ include_all_files.php'); `