Using dbfon.pl

Dbfon.pl is a little 16K perl script that makes it easy to assemble simple web applications. It manages flat data stored in a filesystem, and it uses template files to display and manipulate that data in simple ways. It requires very little configuration to get it going.

The platform requirements are minimal and standard: it uses only perl, and runs on the standard Apache server. (For example, it works great on the server space provided by the $25/mo plan from Hiway.) No database is needed.

The main benefit of dbfon.pl is its simplicity, flexibility, and low platform requirements. It is not optimized for significant transaction volume, but the performance is sufficient for generating large numbers of mostly read-only template pages, or for lightly used interactive pages.

An Example: Macros

Dbfon can be used for macros. (We don't actually use it this way on our web site, but it's a good illustration of how dbfon.pl works.)

For example, if we want to make it easier to insert several icons, e.g., <img src="/images/new.gif"> () into our HTML pages, we could set up a macro file macros.dat containing

new:<img src="/images/new.gif"> improved:<img src="/images/improved.gif"> bullet:<img src="/images/circle.gif"> elogo:<img src="/images/elephant.gif">

Then we can author our HTML pages as templates with macros by saving them with a .tmp extension instead of an .htm extension:

<#header#> <#for "macros.dat"#> <html> <title>Elephant Products</title> <body> <#elogo#> <p><#bullet#>White elephants! <p><#bullet#>Pink elephants! <#new#> <p><#bullet#>Bigger elephants! <#improved#> </body> </html> <#end#>

Dbfon.pl will expand the template file using the macro data as you would expect. The only thing dbfon.pl has done for us is save a little bit of typing. But notice that if we now change the contents of macros.dat, any templates that use macros.dat will automatically expand according to the new definitions.

This ability to display the same data in different contenxts is the key to maintaining any website that must consistently display interconnected information. For example, the title of an article should show up consistently in all editions (IE, Netscape, WebTV) of the newsletter, the index, the newsletter editor, etc. Maintaining all these HTML files seperately would be an impossibility. But by placing in data files key information such as the title of each article, we can maintain the entire web site by simply maintaining the data files. When we change the title in a data file, we can expect it to show up correctly everywhere.

Basic operation

When a query is submitted to dbfon.pl, it supplies a sequence of actions for dbfon.pl to take. Each action is simple: store a value in a data file, move, copy, or delete a data file, or evaluate a template with specific parameters. The final result is the expanded template file or the url of a redirect.

Simple applications built with it dbfon.pl typically consist of a set of HTML template files that operate on a set of data files on the server. Usually, no futher javascript perl is needed. This frees the site author to concentrate on site design rather than programming technicalities. This also means that there is little browser-dependence, and applications run well on very simple clients such as WebTV.

The power of dbfon.pl is in its template evaluation. A template file is like HTML, but it contains macros that allow it to iterate through data files, execute simple logic, and automate data manipulation. As we extend the capabilities of dbfon.pl, most features will be added by adding new constructs to the template language.

Data files

dbfon.pl stores data on the server in data files which store name-value pairs. A single application may use a hierarchy of multiple data files in multiple directories.

For example, a newsletter application might use a data files to describe each story. One datafile's contents could be:

title:Family Visits Disneyland
author:David Bau
text:Last week, we got the whole crowd together and spent...
This datafile has a "title" that is "Family Visits Disneyland", and an "author" that is "David Bau", etc. Users never need to see the contents of a data file; dbfon.pl can always be used to view and manipulate them using the web. Nevertheless, to ease debugging and data interchange, dbfon.pl currently stores data files in a very easy-to-read and easy-to-parse ascii format.

dbfon.pl can change any field of a data file; it can also move, copy, delete, or iterate data files. There are several techniques for doing these things; this is what we describe the sections below.

Template files

The basic way to view data on the server is to expand it using a template file. Template files are simply HTML files that contain macros that are expanded by dbfon.pl. For example, the template file below could be used to view a newsletter story.

<#header#>
<title><#title#></title>
<body>
<h1><#title#></h1>
<#text#>
<address>by <#author#></address>

Notice that the <#...#> constructs are not HTML: those are the macros. Everything else is ordinary HTML.

To set things up, we would store this template in the directory "/tmp/news/viewstory.tmp", and we would store the data for a story in "/dat/news/story1.dat". Then invoking dbfon.pl in this example is simple: just visit the URL "http://www.yoursite.com/cgi-local/dbfon.pl/news/viewstory/story1".

Template files are powerful because they can contain macros that iterate and make decisions. For example, by adding a simple "<#for#>" construct to our template, we can put multiple stories on a single page:

<#header#>
<title>Our Newsletter</title>
<body>
<#for "/story#" #>
  <h1><#title#></h1>
  <#text#>
  <address>by <#author#></address>
<#end#>
If the "/dat/news/" directory contains multiple data files "story1.dat", "story2.dat", etc, they are printed in order.

Template File Reference

Here is a list of the macro constructs recognized by dbfon.pl and how to use them:

Invoking dbfon.pl

dbfon.pl parses both the extra path info and the CGI parameters. The extra path info is used to specify the root directory for the application, as well as the path of the default template and data to be used. (All templates and data for an application are located under the root directory; this allows allows multiple applications to reside on one web site while being segregated from each other.)

The CGI parameters are used to specify a sequence of simple actions that are to be taken before applying the default template. Actions include: storing data, moving, copy, or deleting a data file, redirecting to another URL, or evaluating a template.

Extra path info

The extra path specifies the base directory, template, and data file to be combined. An example: if the request path is "/cgi-local/dbfon.pl/foo/bar/zee/qux/vit", the extra path info is "/foo/bar/zee/qux/vit".

dbfon.pl parses the path, looking for a template directory that exists to designate as the root directory for this transaction. In this example, if /tmp/foo/bar is a directory with no subdirectory "zee", the root directory is "/foo/bar".

Then it looks for a template with the next portion of the extra path info to treat as the default template. In this case, it looks for a template named "/tmp/foo/bar/zee.tmp". If no template with that name exists, "index.tmp" is used.

Finally, it uses the remaining part of the extra path info as a data path which is used to supply parameters to the default template. In this case, the data path is "/qux/vit", which means that the data file "/dat/foo/bar/qux/vit.dat" is used as the default data if present.

In this case, the data "/dat/foo/bar/qux/vit.dat" is loaded and its values are used to evaluate the template file "/tmp/foo/bar/zee.tmp" in the context of the root directory "/foo/bar".

CGI parameters

The name-value pairs passed as CGI parameters are also used to expand the default template (when a datafile and CGI parameters both specify the same value, the datafile takes priority). This is useful for supplying information to a template that depends on the context, rather than just the data stored on the server.

In addition, the CGI parameters can specify a sequence of actions to take before expanding the default template. This is done by specifying CGI parameters whose name begins with a numeral. Each action is specified as the value of a bare numeral; and each action can takes parameters that are passed as numeral and a dot.

For example, if the CGI parameters are:

1=set
1.path=/foo/bar
1.myval=zee
2=redir
2.url=http://www.yahoo.com/

dbfon.pl understands that it must first set to "zee" the "myval" field of the data file at "/foo/bar" (inside the root directory specified by the extra path info). Then it must redirect to www.yahoo.com.

When using this latter technique, the sequence of CGI parameters is usually supplied via hidden fields on HTML forms (<INPUT TYPE=HIDDEN>). Arbirarily long, simple sequences of actions can be encoded this way, without writing seperate script or template files. However, the complexity of this techinque is limited by the fact that no looping or testing is supported. And since this technique uses so many hidden input fields, it can cause HTML files to grow too large. In these cases, the logic should be moved to a template file.

However, using CGI parameters like this allows many transactions to be described without an additional template file at all.

CGI parameter reference

For more...

This is just a start of documentation that I've posted in case anybody is interested in the perl script. I need to document the other 90% of what the script does.

Used correctly, the script is pretty powerful. For example, it allows my parents (who are fairly computer-naive) to author and organize the Bau Family Online News from their WebTV browser, without knowing any HTML or without even using a PC. It also allows me to instantly change the layout of all 30 issues or add little dynamic features like "Quote Quiz" quickly.

If you're interested in using the script, please don't hesitate to contact me at davidbau@hotmail.com; I'll share it with you and write up some more docs.

A real-world example set of scripts

You can take a look at a real-world example set of templates we use to edit a small fantasy football website, below. Note that some web browsers will confuse the template files for HTML, so you may have to save the files and view them in a text editor to see what's going on. (I've provided view-source links that do this in some browsers.)

index.tmp (source)
editstory.tmp (source)
edittop.tmp (source)
editsection.tmp (source)
buildall.tmp (source)
front.tmp (source)
section.tmp (source)