3 ms·
For really simple sites that don't need a full blown database backed CMS I just use simple PHP includes like the following: <?php $title = "Whateve
by tlrobinson 19y ago
For really simple sites that don't need a full blown database backed CMS I just use simple PHP includes like the following:
<?php
$title = "Whatever";
include("header.php");
include("content.txt"); // or just include the content inline
include("footer.php");
?>
Then header.php is just:
<html>
<head>
<title>My Site - <?php echo $title; ?></title>
<!-- other header stuff like css, scripts, etc -->
</head>
<body>
<div id="content">
and footer.php is just:
</div>
<!-- other footer stuff like Google Analytics, etc -->
</body>
</html>
If you want to get a little fancier you can set up a URL rewriting (like Apache's mod_rewrite) to internally change yoursite.com/section/subsection to yoursite.com/content.php?a=section&b=subection or something, then you can eliminate all the duplicate templates and just have your content files (make sure you validate the section and subsection names). Or you could plug in a really simple database.
I'm no PHP expert, so if anyone has better ways of doing this kind of thing I'd love to hear it.
(actually, I have a question: with this layout it's hard to pass data from included files back up to the templates, like $title above needs to be defined before the header include... is there some trick I'm missing?)