Travis
Travis

Reputation: 12379

Any problem with using includes for everything but page-specific content?

I'm developing a site that's pretty lightweight on the interface and mostly database driven (equal amounts read and write). It's written in PHP and I've found the easiest way to make each page is:

Page:

<?php include("header-nav.php"); ?>
  <table>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
    </tr>
    <tr>
      <td>Data Point 1</td>
      <td>Data Point 2</td>
    </tr>
    <tr>
      <td>Data Point 3</td>
      <td>Data Point 4</td>
    </tr>
  </table>
<?php include("footer.php"); ?>

header-nav.php:

<!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">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Welcome</title>
    <link rel="stylesheet" type="text/css" href="assets/style.css" />
  </head>
  <?php include("db_connect.php"); ?>
  <body>
    <div id="wrapper">
      <h1>Welcome</h1>
      <ul id="nav">
        <li><a href="index.php">Home</a></li>
        <li><a href="data.php">Data</a></li>
      </ul>

footer.php:

    </div>
  </body>
  <?php mysql_close($dbc); ?>
</html>

All of these pages by themselves are not valid and may produce errors. Working together to create a whole page, they look great.

My question is: Would this be an efficient and easy to maintain solution?

It seems that just adding a couple includes (one at the top and one at the bottom) makes sense. Especially because the site is so lightweight that I don't need a web framework. It just feels uncomfortable to create pages without starting with <html><head>...etc.

Upvotes: 0

Views: 111

Answers (4)

razzed
razzed

Reputation: 2683

Get over your discomfort. Most IDEs (Dreamweaver comes to mind) actually support this way of developing sites, and will display content correctly and honor the includes if you prefer a WYSIWYG.

I develop sites using includes, like so:

site-header.inc:

require_once 'html-header.inc';
<div id="header">
/* menus, navigation, etc. */
</div>
<div class="content">

site-footer.inc:

</div>
<div id="header">
/* menus, navigation, etc. */
</div>
require_once 'html-footer.inc';

Where "html-header.inc" and "html-footer.inc" are your HTML header and footer tags and elements (title, meta, etc.). I then have functions to allow me to add CSS, JavaScript, titles, anywhere on the page, and use ob_start() and ob_end_flush() to handle these in the footer, actually. e.g.

stylesheet_register($path, $media="screen", $type="text/css");
javascript_register($path, $type="text/javascript");
title_set($title, $overwrite=true);

It your basic concept of abstraction: Don't write the same "header" and "footer" HTML code twice. Same applies to any PHP functionality which can be easily abstracted away and decoupled. Best of luck.

Upvotes: 0

James
James

Reputation: 3275

"All of these pages by themselves are not valid" - I'm not sure what you mean by this. You mean a HTML Validator wouldn't pass them? Well of course not - they are fragments of pages. What matters is what the validator says when ran against the HTML the executed PHP generates.

This is one approach, and depending on the size of the problem you're tackling it's a valid one.

Upvotes: 1

Eric Petroelje
Eric Petroelje

Reputation: 60498

Yes, IMO this is a perfectly good way to do things, especially for a small site. Done it myself many times.

Upvotes: 0

Boushley
Boushley

Reputation: 7036

This is definitely an okay thing. I would highly recommend it. This way if you need to change the header or anything you can do so in once place easily. And the read time for hitting the file system for the include really isn't that big of a concern. So I would say that this is definitely acceptable.

Upvotes: 2

Related Questions