如何将我的内容放在正确的位置和所有页面?

时间:2016-02-15 19:41:30

标签: php html

我需要帮助。我正在研究我的结构,但我遇到了一些问题...

我创建了一个名为“page-container.php”的页面,内容为:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title><?= $page_title; ?></title>
        <link href="css/style.css" rel="stylesheet">
        <link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
    </head>
    <body id="<?= $page_id; ?>">
        <?php include "includes/header.php"; ?>
        <div class="wrapper">
            <div class="page-content">
                <?php if(isset($sidebar)): ?>
                <div class="main-container">
                    <div class="main-content">
                <?php endif; ?>
                <h1 class="page-title"><?= $page_title; ?></h1>
                I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES
            </div>
        </div>
    </body>
</html>

你可以看到“我希望我的所有页面都有我的内容”。

我的意思是,当我在其他页面中写入内容时(例如,我的注册页面),我希望所有内容都放在这个位置,如下所示:

http://prntscr.com/a3oz5k

我的所有其他页面都是这样的(例如index.php):

<?php

$page_title = "Home";
$page_id = "home";

?>

<?php require "includes/page-container.php"; ?>

如果我想在此页面中写一些内容,我需要这样做:

<?php

$page_title = "Home";
$page_id = "home";

?>

<?php require "includes/page-container.php"; ?>

<div id="mycontent">
<p>Heellloo</p>
</div>

但看看结果:http://prntscr.com/a3p32y

在代码来源中,这不正常...... http://prntscr.com/a3p3kg

我的内容低于html和body标签,我希望它放在我的h3标题下面。我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用ob_start()ob_get_clean()

的简单示例
<?php
ob_start();
?>

<div id="mycontent">
<p>Hello</p>
</div>

<?php
$page = ob_get_clean();
require "includes/page-container.php";
?>

然后在page-container.php只做echo $page;。 如果代码存在于变量中,您的代码仍将在编辑器中格式化为HTML。

您还可以将page-container.php拆分为两部分,并将第一部分包含在内容之前,将第二部分包含在内容之后。

参考:ob_start()ob_get_clean()

答案 1 :(得分:0)

你可以保留另一个vartiable say $ page_content并使用如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><?= $page_title; ?></title>
    <link href="css/style.css" rel="stylesheet">
    <link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
    <?php include "includes/header.php"; ?>
    <div class="wrapper">
        <div class="page-content">

            <h1 class="page-title"><?= $page_title; ?></h1>
            <?= $page_content; ?>
        </div>
    </div>
</body>
</html>

对于另一页中的某些内容

<?php

$page_title = "Home";
$page_id = "home";
$page_content  = "content";

?>

<?php require "includes/page-container.php"; ?>

`