In this fourth 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)
{ $title=$items[0];
$url=$items[1];
$post=$items[2];
$tags=$items[3];
$description=$items[4];
$author=$items[5];
$date=$items[6];
}
else
{ <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.
$fp=@fopen($config_theme,"rb");
if($fp!==false)
{
if( false !== ($data=fread($fp,filesize($config_theme))))
{
$data=str_replace("{title}",$title,$data);
$data=str_replace("{post}",$post,$data);
$data=str_replace("{tags}",$tags,$data);
$data=str_replace("{description}",$description,$data);
$blog='';
if($author!='' && $date !='')
{
$blog="Posted by ".$author." on ".date($config_date,$date);
}
$data=str_replace("{blog}",$blog,$data);
echo $data;
}
fclose($fp);
}
else
{
echo 'Internal error'; }
Makes sense? Hope so, because there’s not much more to be done. Save the script as index.php, then create or edit the .htaccess file. We need a rewrite rule to pass everything the user requests to index.php.
The .htaccess rewrite rule
Now, if there’s something I really don’t like to do it must be writing rewrite rules. I’m no Apache guru and have never studied them formally like I did for programming. But this one is really simple to use and understand.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First, we turn on the rewrite engine and say everything is relative to the web root.
Then, there are two rewrite conditions, almost identical. They check if a file (-f) or a directory (-d) exists. If so, the existing file or directory is served.
Otherwise, the rewrite rule is invoked, and points to /index.php, our code. Note that no redirection is performed, so this will return an OK 200 status and the user’s browser will still display the originally requested url.
That’s almost all, folks
Okay that’s all. The simple cms is ready. However it’s still a bit poor, even adding a nice artisteer template it will have no navigation. We may want to add RSS, a sitemap, even some widgets. As long as there’s no user submitted content, like comments, we can do everything without writing to our server. And we’ll see how in the next post.
Feel free to comment, ask questions and suggest how to expand this simple flat file php CMS. I’m not sure it’s worth to add an administrative panel, as this is really meant to be a set and forget script, but will wait for your suggestions.
Thanks for reading so far, have fun!