Wednesday, February 15, 2012

Learning how to programmatically generate webpages

Over the course of my life, I've worked with a variety of scripting languages to automate tedious processes. I generally detest tedious work because it is both a waste of time and not intellectually stimulating so I've written engines on top of AutoCAD and 3D modeling tools to automate the generation of complex objects using simple commands which are easy to represent in code and gets translated nicely into commands for interpretation... and so I am back at it again with trying to programmatically generate html sites and content.

The difference between generating scripts and html

Generating dynamic html pages is a very old problem on the internet, one that was first tacked by CGI scrips by the likes of PHP. I never got into the world of PHP scripts because I've always felt that the languages were so limited and ugly looking that I abhorred looking html code. Even the html code for many modern websites can look pretty nasty. The number of attributes per tag and more and the incredible amount of parameters that ones needs to jiggle about is a pain in the rear. Many of those problems have been solved with the advent of Cascading Style Sheets (CSS) which are used to maintain style and formatting through out a website.

I've started looking at templating engines online to learn the frameworks people use to generate html pages and have been pouring through lots and lots of documentation. Most documentation will describe basic syntax and provide a few examples to get you on your way. The biggest thing I generally think about is if doing something a certain way is the most efficient way of doing it.
Since Python has become my mainstay language, I've been looking into templating libraries for Python (for which there are many) and have seen that many of these languages like to embed Python code into content, which is a similar track to what PHP does by embedding PHP code using commented out html and writing code into content. The biggest difference between writing script generating code is different because one generally does needs not worry about "content" and simply needs to worry about generating sequential commands with different parameters. The highly structured nature of writing scripts makes code to generate scripts very easy, where as html content follows no such rule.

To give an example, perhaps I might define commands to generate a 3D object using the following commands:

def boat(width, length, height, location):
hull(width, length, height)
anchor()
motor()
mast()
shift(location)

and then I would simply call the boat function a few times to create a fleet of boats at different locations with varying sizes.

Using code in this manner is great to generate results with small permutations with respect to a general form. The same thing, unfortunately, does not transfer well to text content because text content is not easily generalized to a simple form with small permutations. Most text content is unique and writing a function to generate each and every paragraph is rather meaningless since the main purpose of writing functions is to shrink repetitious tasks into short commands to make life easier.

Since using code to generate text is unfeasible, the best place to use code in a website is to aim for the most structured portions of a website, which are the database components and formatting of content where the application of code is most likely to shine. There still is the ugly part of dealing with html. Should html be 1) completely generated by external functions or 2) should code be embedded into html to generate content? I still haven't figured which is the best way of tackling this problem just yet, but I think embedding python code into websites is ugly and I would much rather have structured data generated outside of an html template and injected later on.

It just means that I have some experimenting to do before adopting a templating framework.

No comments: