Simple CMS
I had the idea to simplify my coding for content sites in a similar way to content management but without using a database. So here is the solution I came up with.
An important fact that I considered was to make all the site links search engine friendly with keywords in URLs.
You can see from this code that it is very easy to make site-wide changes to the site even if you had 100+ pages. This is because, each page only differs by the page title and content.
However, you may want to move the footer outside of the content area in your implementation.
This CMS uses PHP code to insert common blocks of HTML into each page such as the header, footer and navigation links.
Page Template
Each page is based around a template like this:
<?php
define("_PAGE", nn);
include_once("common.php");
?>
<title>Title Text</title>
<?php get_meta(''); ?>
<div id="content">
<h1>Header Title</h1>
<p>Page content.</p>
<?php get_footer(''); ?>
</div>
<?php get_end('1'); ?>
</body>
</html>
So to create a page just give it a number nn, edit the Title Text and add the Page content which is the usual HTML code you have in a web page.
Save the new page as PageName.php replacing PageName with relevant keywords. If it is the home page, the file name must be index.php
Common File
This is the file that inserts the common code:
<?php
include_once("db.php"); //this is an optional file for database functions
header("Content-type: text/html; charset=utf-8"); //the character set
echo '<!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>
'; //this inserts the XHTML header
//select a meta section (before the content starts)
function get_meta($i){
switch ($i) {
case 1:
echo "Nothing!";
break;
default:
echo'<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>';
}
}
//select footer code
function get_footer($i){
switch ($i) {
case 1:
echo "Nothing!";
break;
default:
echo'<div class="footer">© yoursite.com</div>';
}
}
//code to insert after the content area
function get_end($i){
global $nav1, $nav2, $nav3;
echo'
<div id="nav">
';
if ($i != 1) echo $nav1;
if ($i == 2) echo $nav2;
if ($i == 3) echo $nav3;
echo '</div>
<span class="logo">
<a href="./"><img alt="logo" src="images/logo.gif" height="y" width="x" /></a>
</span>
';
}
$nav1 = "<a href=\"./\">Home Page</a><br />
";
$nav2 = '
Main Links
<div class="mainNav">
<a href="link1">Link1</a>
<a href="link2">Link2</a>
<a href="link3">Link3</a>
</div>
';
$nav3 = '
More Links
<div class="extraNav">
<a href="linka">Linka</a>
<a href="linkb">Linkb</a>
<a href="linkc">Linkc</a>
</div>
';
?>
Save this file as common.php and customize it with your navigation links.
Note that the functions act on a value passed to them (e.g. 1,2,3) from the web pages. If no value is passed then the default result is output.
The remaining task is to create a style sheet and save it as styles.css to control the appearance of the page elements.
In this example, we didn't use the nn value defined on each page. This is for when you have a script in the common.php file so it can use this value and know which page is being accessed. For example, people are rating an article or leaving comments on a page.
To understand the basics of PHP, please visit: PHP Documentation
Example Sites
If you have used this simple CMS concept for your site, let me know via my blog and I'll consider placing a link to it here:
Sites using this simple CMS: