Simple Flat File CMS in PHP – Part 3

In this third post, we’ll have to actually do something useful with our code. Simply add the following at the end of the previous script.


if($found)
{// set up variables with content
 $title=$items[0];
 $url=$items[1];
 $post=$items[2];
 $tags=$items[3];
 $description=$items[4];
 $author=$items[5];
 $date=$items[6];
}

else

{ // if we get here there was a 404 error either for malformed url or because we couldn't read the file
 $title="Page not found";
 $url="404.html";
 $post='<h1>Ooops! Page not found</h1><p>Don\'t worry, this may happen. Please go back to the <a href="'.$config_site.'">Home Page</a>.';
 $tags="Not found, 404";
 $description="This page could not be found";
 $author="";
 $date="";
}

Now we have everything set up. We only need to populate our theme with the relevant content. We’ll use psuedo tags in the theme, like {title}, {post} and so on.

We’ll place those meta tags in our template, or the Artisteer template. Then save this file as “theme.html” or whatever name you prefer to use.

Here’s an extra simple theme with no styles, just to show the concept:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content="{description}" />
<meta name="keywords" content="{tags}" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<link href="put-your-css-here.css" type="text/css" rel="stylesheet" />
<link href="i/favicon.ico" rel="SHORTCUT ICON" />
<script type="text/javascript" src="put-your-javascript-here.js"></script>
</head>
<body>
<h1>{title}</h1>
<h2>{blog}</h2>
{post}
</body>
</html>

This approach is slower than echoing the variables in the page itself, as it requires reading the theme and performing string replacements, but gives us a clean separation between the theme and the code, and makes this project easily scalable.

Next time we’ll see the actual replacement code, the url rewriting and we’ll have a working solution.

Any question? Just drop a comment!

3 Responses to “Simple Flat File CMS in PHP – Part 3”

  1. Jason Says:

    loving this series – when can we expect the next post?


  2. admin Says:

    Ooops! Thanks for pointing this out, third post became too long and I split it in a few more, then forgot to add them. Part four is out now, and the mini cms is complete. But a fifth is planned to add some more bells & whistles.


  3. Jason Says:

    Thanks for this – you’re the best.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>