Using PHP to split site content

I am looking for best practice tips for dividing site content into logical blocks. I want the header and footer to be constant throughout the site, so if I have several pages with different content, they will all look like below - changes made in the header and footer, and then automatically update without me, to change each individual page.

<?php
include 'header.php';
?>
<body>
    <p>page content here</p>
</body>
<?
include 'footer.php';
?>

header.phpIt will contain an opening <html>, <head>and the static content, and footer.phpwill contain a further static content, and a closing tag </html>. So my question is: is this a good approach? I worry that tagging <html>across multiple files is bad practice. If so, what is the right approach to such a design?

+5
source share
4 answers

No, your approach is wrong.
Here are the main mistakes in your design:

  • You assume that the header.php header will be called every time it is called on the page. It is not right.
  • You assume header.php will always be static. It is not right.
  • You forgot to create a template for the page itself.

, :

, .

?

  • 2011. AJAX . , JSONed HTML-?
  • , HTTP header. . , HTML-.
  • 4- . . , 4- . . .
  • , <title> . ? .

, , , php script.

:

0,1. .

, :

<?php
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>

0,2. template.php, ,

:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<?php include $tpl ?>
</div>
</body>
</html>

0,3. , , links.tpl.php :

<h2><?=$pagetitle?></h2>
<ul>
<?php foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<?php endforeach ?>
<ul>

, .

+13

Your Common Sense 2 . (YCS, .tpl.php), .

, :

<?php
#lib/PageTemplate.php
class PageTemplate {
    public $PageTitle;
    public $ContentHead;
    public $ContentBody;
}

:

<?php
# layout.php
require_once('lib/PageTemplate.php');
?>
<!DOCTYPE HTML>
<html>
<head>
    <title><?php if(isset($TPL->PageTitle)) { echo $TPL->PageTitle; } ?></title>
    <?php if(isset($TPL->ContentHead)) { include $TPL->ContentHead; } ?>
</head>
<body>
    <div id="content">
        <?php if(isset($TPL->ContentBody)) { include $TPL->ContentBody; } ?>
    </div>
</body>
</html>

, , :

<?php
#Hello.php
require_once('lib/PageTemplate.php');
# trick to execute 1st time, but not 2nd so you don't have an inf loop
if (!isset($TPL)) {
    $TPL = new PageTemplate();
    $TPL->PageTitle = "My Title";
    $TPL->ContentBody = __FILE__;
    include "layout.php";
    exit;
}
?>
<p><?php echo "Hello!"; ?></p>
+7

, , , :) , ,

, ;) , . , , . , / , , , , .

, , . MVC. . , ?

+4

index.php - , REQUEST.
header.php -
footer.php -

content1.php, content2.php ..


index.php:

<?php
include ('header.php');

// VERY IMPORTANT - do not use the GET variable directly like this
// make sure to filter it through a white-list
include(basename($_GET['page']).'.php');

include ('footer.php');
?>

if you want the URL to be sent to www.domain.com/pagename, where the page you are trying to load in index.php is "pagename", use HTACCESS and rewrite the URL: http: // corz. org / serv / tricks / htaccess2.php

-6
source

All Articles