Webpage-1.0 Server side web page generation

The webpage package is a tool that can be used to generate server side pages in HTML using CGI scripts written in TCL. The package makes use of templates which are expanded using a set of parameters within a Tcl interpreter.

The templates are normal text files that contain normal HTML statements and optionally markups that implement the features of the webpage package.

The markups are statements of the form:

!markup PARM=value; ...

where markup is one of the following:

  • !include
  • !defaults
  • !defines
  • !redefine
  • !repeat
  • !duplicate
  • !foreach
  • !if
  • !else
  • !endif
  • !end
  • !switch
  • !return
  • !comment
  • !echo
  • !exit

so, for example, a template within a file named my-input.html such as:

# --- my-input.html --- An example of a parameterized template

!comment Including my-input.html

<li style="margin-left:10px; width:350px;">
!if "TYPE" == "submit"
<label> </label>
<input style="float:right" type="TYPE" value="VALUE" form="FORM_ID" />
!else
<label>LABEL</label>
<input style="float:right" type="TYPE" name="NAME" id="NAME" form="FORM_ID" value="VALUE" 
        required placeholder="Enter LABEL (Required)"/>
!endif
</li>
<br>

!comment End of my-input.html

might be used in a parent template named my-page.html such as:

# --- my-page.html --- Generate some inputs

# This demo uses several of the features of the webpage package to generate an HTML page
# that demonstrates to use of markups, heirarchical parameter passing, embedded Tcl command
# interpretation, and documentation with both HTML comments and non-HTML comments. 

!comment Including my-page.html

<!doctype HTML>
<html>
<head>
<title>TITLE</title>
<meta name="Author" content="AUTHOR"/>
<meta name="Generator" content="[file tail [info script]]" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
<meta name="Generated" content="[clock format [clock seconds]]"/>
<meta name="Copyright" content="Copyright(C) AUTHOR, 2018-[clock format [clock seconds] -format %Y]"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="background-color:[FindColor LightGrey]">

# Set some default values

!defaults ITEMS=name&Your Name: + address&Your Address: + phone&Your Phone: + passwd&Your Password:
!defaults FORM_ID=myform; VALUE=; TYPE=text

# Generate a form for some inputs

<h2>TITLE</h2>

<p>All the fields are required. You need a script to process the input on a server if you wand to click the submit button. The script on the demo server does nothing!</p>

<form action="/cgi-bin/ACTION_SCRIPT.cgi" id="FORM_ID"method="post">
<ul style="list-style-type:none">
!foreach ITEMS
!if "P1" == "passwd"
!include ../html/my-input TYPE=password; NAME=P1; LABEL=P2
!else
!include ../html/my-input NAME=P1; LABEL=P2
!endif End of if clause
!end End of foreach loop
!include ../html/my-input TYPE=submit; VALUE=Submit my form to the server
</ul>
</form>
<hr>
<div>Copyright(c) AUTHOR 2018-[clock format [clock seconds] -format %Y] All Rights Reserved</div>
<div>Last updated [clock format [clock seconds]]</div>
</body>
</html>

The template above could be expanded in the following CGI script:

#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
## --- index.cgi --- Generate a sample server side web page

package require webpage

puts "Content-type: text/html\n"

puts [::web::LoadHtmlPage ../html/my-page "TITLE=An example of a web page using the webpage package; AUTHOR=Iain B. Findleton; ACTION_SCRIPT=process-form"]

So, you can see the result of this demo at [L1 ]

The current version implements a cache mechanism that eliminates the need to reload template files from disk, greatly improving the performance of page generation when templates are reused many times.

The webpage package can be found at [L2 ] and the detailed documentation can be found at [L3 ].