MindPalette Discussion Forum

Server Side Includes

MindPalette tutorials and scripts

Server Side Includes

Postby ch on 11/24/2008 @ 20:25

Hi All

I run a small website that uses PHP pages it started of small 5 odd pages, it has now as these things do grown to 30 odd pages.

I am now cosidering useing Server Side Includes to make updating the entire site easier. I would have one page called top.php one called bottom.php. The top page would carry all the header info and the bottom would carry all the footer stuff. I would then use the following code to generate the page

<?php require_once('top.php'); ?>

Then page content

<?php require_once('bottom.php'); ?>


Does anyone have any comments pros/cons etc

CH
ch
 
Posts: 18
Joined: 08/14/2007 @ 07:24

Re: Server Side Includes

Postby Nate Baldwin on 11/25/2008 @ 10:59

That should work fine in most cases. I usually handle my templates a little differently so I can have just a single template file with unlimited editable regions, making heavy use of the output buffer. Here's an overly simplified example of a page based on a template:
Code: Select all
<?php
// build page-specific head content
ob_start();
?>
<title>My Page Title</title>
<link rel="stylesheet" type="text/css" href="styles.css" media="all" />
<script type="text/javascript" src="stufftodo.js"></script>
<?php
$headContent = ob_get_contents();
ob_end_clean();

// build page-specific body content
ob_start()
?>
<h1>My Page Heading</h1>
<p>Here's some fine body content.</p>
<p>[ <a href="wherever.php">Here is a LINK.</a> ]</p>
<p>Hwew's a footer or something.</p>
<?php
$bodyContent = ob_get_contents();
ob_end_clean();

// import the template and insert content
include('path/to/template.php');
?>


...and here's an overly simplified example of the template file:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <?php print($headContent); ?>
</head>
<body>
   <?php print($bodyContent); ?>
</body>
</html>
Nate Baldwin
Site Admin
 
Posts: 3724
Joined: 04/25/2003 @ 19:05


Return to Tutorials and Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron