This is the second part of the story about this site in all plain html that needed some revamp. Yesterday, I talked about installing PunBB as a forum, and today, I'll talk about a quick templating.
I think the webmaster used tools like Dreamweaver to build the site. My friend asked me to move one image in the footer of the page. That image was inside a
<table>, moving it for one page was not a PITA, but repeating the steps for all .html files would really suck. Other options would be to create a script to do this automatically, or restart from scratch with a templated system. Finally, I found a dirty quirk to do the job, and without changing any file present in the site. It's really simple, I redirect all URL in .html into a script mytemplate.php, passing the filename as a parameter, and do everything in mytemplate.php.
.htaccess to perform the redirection:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^([^/]*).html$ mytemplate.php?page=$1 [L] RewriteRule ^/?$ mytemplate.php?page=index </IfModule>
Second step is to copy index.html into mytemplate.php. I stripped all content, leaving only what would be in a template, ie:
<head>, header, footer and sidebar part of
<body> tag. mytemplate.php is now my template for all .html file. What I removed was replaced with some simple PHP instructions which load the content of the page :
The first instruction is a security measure, just in case someone tries to exploit this template with URL like /mytemplate.php?page=/etc/passwd (although the 2nd line allows only .html file to be read). The second line loads the .html file. The 3rd and 4th lines strip headers and footers, because they are now provided by the template mytemplate.php. Looking for the pattern was the hardest thing to do, but if some of the html files missed these pattern, I could add them manually. And that's it. For any change I need to do, I have to play with mytemplate.php only. The other good surprise is that I can mask all the changes I've made just by disabling the rewriting in .htaccess.
<?php if (preg_match('![/\\:]!', $_GET['page'])) return; $k = file_get_contents($_GET['page'].'.html'); $k = preg_replace('/^.*<!-- end of menu -->/sm', '', $k); $k = preg_replace('!^\s*<!--start of footer -->.*$!sm', '', $k); echo $k; ?>