Friday, December 10, 2010

The Problem
I develop websites on the side for some spare cash. I have built some sites with amazing functionality in the past. Things like GD drawings with code, log ins, user profiles, news portals, RSS feeds, shopping carts and other awesomeness are possible with PHP and I have built those widgets and incorporated them into websites.
The problem with all of that bling is that it isn't necessary when I'm building a simple informational website for a business like an excavator, a home builder or a company that rents roll-away dumpsters to people.
“Brochureware” is the simplest form of website there is. It is just static HTML that does not require updating all that often. If you have ever taken an HTML programming class, these types of sites are all you are taught to produce. These sites and the code behind them are really the basis for the rest of the internet as well.
Here is a comparison between dynamic and static site programming:

Dynamic Static
Site-wide changes replicate automatically to all pages Site-wide changes are a time-consuming and repetitive task
Formatting is strewn among scripting code. Less simple, not easy to get right Formatting is simple and easy
Dynamically changing content Hard-coded content
Heavy on the server-side with respect to resources Processing is all on the client side in the web browser
The Solution
How can I have the benefits of both when building a website? How can I have a light, brochureware site but program the site dynamically – replicating formatting changes throughout the site? How can this possibly be simple? How can I provide clients with timely site-wide updates?
This week I came-up with a solution.
How I did it was I used a PHP framework that I had laying around that uses PEAR templating for formatting, a bunch of fancy scripts to present data based on certain logical conditions and pull all of the text from a mysql database. This version of the site is only available on my local dev machine. Then I ran the following script to wget all of the pages presented on the localhost, rename the pages accordingly to give them the html extension, then find and replace certain text in the html code pulled in order to make the relative links work properly. Get all of that?
The script:

#!/bin/bash
folder="/home/justin/scratch/localhost/site_builder"
wget -r -p -k localhost/site_builder/index.php
for i in `find $folder -name "index.*"`
do
page=`echo $i | cut -d= -f2`

if [ "$page" == "home" ]
then
page="index"
mv $i $folder/$page.html

elif [[ "$page" =~ "/" ]]
then
something=1

else
mv $i $folder/$page.html
fi

done
sed -i 's/index.php?i=home/\//g' $folder/*.html
sed -i 's/index.php?i=//g' $folder/*.html

for i in `find $folder -name "*.html" | cut -d/ -f7 | cut -d. -f1`
do
echo $i
sed -i "s/$i/$i\.html/g" $folder/*.html
done
sed -i "s/http:\/\/localhost\/site_builder\///g" $folder/*.html
rm $folder/index.php



For example purposes, here is where this technique was used.
Reactions:

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!