2012-01-06 - Automation
Automation is basically awesome, especially when you're the one automating stuff. You get to spend hours and hours having fun working out how to automate things and then you can sit back and relax knowing that you no longer have to do some other tedious or repetitive work because the automation now does that work for you all by itself. It’s like automation is converting boring time into fun time all while saving you time. It’s a very good feeling.
Anyways, the reason I’m talking about automation is because I just redid a portion of the site. If you look at the URL of this page and several others you will notice that it says .php instead of .html. The reason for this is that the page you are viewing has actually been constructed on the fly from several different files using a php script. The first file it uses is the template.html file which will eventually be used as the basis for every page on this site. It contains the basic layout as well as the navigation buttons on the side and the logo at the top. If you are viewing this as an individual post the next file loaded will be the post#.html file where # is determined by the ?post=# parameter set in the URL. For example this text will be loaded from post0027.html. After that is completed it starts loading from post.html which is the template for the post page. That page contains javascript which will consult post#.xml (post0027.xml) to get the list of comments for the specific post as well as the comment form for adding new comments which uses another php script to append the new comment to the xml file.
If you are viewing this page on the index then things are done slightly differently. The main template is still used to ensure that things are consistent but the index.php script doesn’t specify which posts to display, Instead it gets a list of posts by finding all files of the format “post*.html” in the posts folder. Once it has this list of files it displays the contents of the last four files in the directory which (assuming it finds the files in increasing order which so far seems to be the case) will been the latest four entries. The archive.php script finds all the files the same as the index but instead of displaying just the latest ones it goes through every file one by one. It first opens the file to get the first line which contains the title and date and then uses this information to create the headings and links.
The reason I’ve added these scripts is, as you may have guessed, automation. My goal was to make it so that if I wanted to change something I only had to modify one file and have changes to that one file automatically get displayed on the relevant pages. This is where the templates come in. To change the logo I simply change it in template.html. To change something about how comments are displayed I simply change it in post.html and it will be reflected on all the post pages. The scripts read these files so changing them will cause different information to be displayed by the scripts. There is also some automation in how some pages display content now. The Index and archive pages use the files in the posts folder to determine their content and therefore don’t require me to edit them every time I post something new. If everything works when I add a new file the scripts on these pages should automatically find it and display it as necessary.
See how fun automation is?
Comments: