Thinair Menu

Menu

Open Source Software: Tcl Web Gen

Tcl Web Gen is a small set of tools for developing websites as simple or complex as requirements demand. Like Tcl itself, TWG tools are a foundation meant to be built on, so all kinds of websites are possible. To date TWG has met the goal of producing fully static sites. Incorporating dynamic content is perfectly feasible via AJAX calls and Javascript output to page elements, it's simply another extension or addition to the toolset that currently exists.

Illustrating some of what can be done, this entire website was produced using only Tcl procedures expanded from TWG tools. Of course we know building a site isn't a one-time operation, that is, generating all or part of site pages has to be done frequently in order to grow the site and maintain the content.

TWG is easily scripted to render all pages from a single proc call. Once the up front work in design and initial implementation is completed and structure is established it becomes trivial to regenerate all or part of a site whenever necessary.

Tcl is a wonderful language to use for this purpose, given its malleability and stability. Performance is not a huge factor for purposes of site generation. Nonetheless despite complexity of some pages, operations are surprisingly brisk, for example, it takes ~850 millisec to generate all 44 pages of this site (~750ms without display writes). That's quite satisfactory for a program of this kind.

Download TWG

TWG latest revision: v0.8.7 (current as of 2018-03-24)
License: BSD (Same as Tcl. See license.txt)

HTML Templates

TWG is based on parsing an HTML template expressed as a Tcl list. This approach was very much inspired by the use of list templates in Lisp/Scheme. Getting this idea to work proved to be a more difficult task in Tcl than Scheme, largely because in Tcl strings and lists are not distinct types. Nonetheless the Tcl version can be made to work effectively, retaining much of the elegance of its Lisp-like nature.

The basic outline looks like this:

{html
  {head ...}
  {body ...}}

HTML tags are the first element of the Tcl list. So adding a headline to the body:

{body
  {h3 {! My Wonderful Site}}}

Note that there are no closing tags. When the list is parsed the closing tag, if it's required, is provided automatically. This makes the markup more clear.

The h3 tag is followed by {! ... }. The "!" is obviously not an HTML tag. In fact the "tag position" in templates is not restricted to official HTML tags but can be nearly anything. Here "!" has a special meaning, that the rest of the list is taken literally and inserted into the resulting HTML as is. (There are cases where additional quoting would be needed as Tcl rules require.)

Another special "tag" is @. Placed right after an HTML tag, {@ ...} signifies an attribute list. Attributes are given as pairs, where the attribute name is followed by its value. For example,

{div {@ class myclass id topdiv} ...}

The template structure is made useful by inserting replaceable content in the form of variables or procedure calls. This is easily accomplished by using the Tcl command.

set ttl "The Great Site"
proc mk-top-headline {ttl} {
  return "Introducing: <em>$ttl</em>"
}
[subst {{html
  {head
    {title {! $ttl}}}
  {body {@ onload pageinit()}
    {h3 {! [mk-top-headline $ttl]}} ... }}}]

When rendered (described below) this example produces:

<!DOCTYPE html>
<html>
  <head>
    <title>
      The Great Site
    </title>
  </head>
  <body onload="pageinit()">
    <h3>
      Introducing: <em>The Great Site</em>
    </h3>
    ...
  </body>
</html>

The Toolkit