The Lua Language
A library of HTML constructors

what is · download · reference · examples · news · implementation

What is HTK?

HTK is a library of Lua constructors that create HTML elements. It was designed to be used within CGILua , a Lua interpreter which provides an appropriate environment to run CGI scripts written in Lua.

HTK was developed to provide a structured way to build HTML pages. It has an homogeneuos API to all HTML elements despite their own particularities: all pre-defined form-field values are set with value attribute, even for multi-line fields (built with TEXTAREA constructor) or radio buttons (built with RADIO constructor).

Download and installation

The current version of HTK source code can be freely downloaded from the following link: Older versions can be downloaded too: HTK doesn't require an installation procedure: it can be required (Lua 5.x) or dofiled (older versions of Lua).


HTK constructors can be divided in some groups:

Virtual Constructors

htk.FRAME makes a rectangular frame around the elements inside it.
htk.MULTILINE creates a text input element with multiple lines (the same as a TEXTAREA element).

Input Constructors

All these constructors are short forms of the INPUT with the type attribute previously set.

Container Constructors

These are constructors that group together some elements of the same type. In a group of radio buttons only one of them can be checked; a group of options compound a selection list.
htk.RADIO creates a group of radio buttons.
htk.TOGGLE_LIST creates a group of toggles.
htk.SELECT creates a selection list.

Compound Constructors

htk.COMPOUND generic combinator of elements.
htk.DATE creates a day-month-year date field: the day-field is a two-character text field; the month-field is a selection box; the year-field is a four-character text field. The default value format can be "dd/mmm/yyyy" or "yyyy-mm-dd"

Table Cell Constructors

Both constructors extends the correspondent HTML elements to accept face , size and color attributes and construct a FONT element inside the cell that will receive these attributes.

Table Row Constructors

The following constructors create a table row with two cells, the left one with a "label" (" label attribute) and the right one with the correspondent input element.

Default value for class attribute

HTK also provides a way to easily add a default value for the class attribute: the table class_defaults can store the value of the class attribute for each element. It is indexed by the name of the element and its value will be copied to the resulting element if there is no definition of the class attribute.
For example, the following code:
htk.class_defaults.TD = "common"
print (htk.TR {
	htk.TD { "first" },
	htk.TD { "second", class = "special" },
	htk.TD { "third" },
	separator = "\n",
will generate the following output:
<TD class="common">first</TD>
<TD class="special">second</TD>
<TD class="common">third</TD>


Here are some small examples of how to use HTK. The following table show three columns, the first one with the Lua source code, the second with the HTML source code generated by the library, and the third with the rendered HTML code.
Lua sourceHTML generatedFinal result
print(htk.B { "Bold" })
print(htk.BIG {
  separator = "\n",
  "A sample ",
  htk.EM { "text" },
  " with some ",
  htk.B { "formatting tags" },
  htk.BR {},
A sample 
 with some 
<B>formatting tags</B>
A sample text with some formatting tags
print(htk.FORM {
  method = "POST",
  htk.TABLE {
    separator = "\n",
    border = true,
    htk.TEXT_FIELD {
      label = "Full name",
      separator = "\n",
      name = "name",
      value = "Write your name here",
    htk.RADIO_FIELD {
      label = "Sex",
      separator = "\n",
      name = "sex",
      options = {
        { "Masc.", value = "M" },
        { "Fem.", value = "F" },
<FORM method="POST"><TABLE border>
<TD class="common">
Full name
<TD class="common">
<INPUT value="Write your name here" type="text" name="name">

<TD class="common">
<TD class="common">
<INPUT value="M" type="radio" name="sex">Masc.</INPUT><BR>
<INPUT value="F" type="radio" name="sex">Fem.</INPUT><BR>

Full name
Sex Masc.
This page was completely generated by HTK -- is its main test! The source code of this page can be downloaded by clicking here



Originally, HTK was based on HTMLToolkit , a set of constructors that reflect in Lua exaclty how HTML elements are built. This approach brought all HTML's heterogeneity to the toolkit. For example, a default value of a TEXT field is set by the value attribute, but if the element is a TEXTAREA , the field with index 1 must be set with the default value; also, if the element is a SELECT , the corresponding OPTION element of the selection list must have a selected clause. So, HTK was built to be the homogeneous interface between Lua and HTML.

But with version 1.X, the programmer must know what constructors are from HTK and what are the original from HTMLToolkit to write it down properly. Now, with version 2.0, HTMLToolkit was eliminated and all its constructors were incorporated into HTK.

Another difference to version 2.0 is the way the constructors are build. HTMLToolkit has a description table for each HTML element, almost always with the same contents; and also, the elements are always created as tables, its contents are checked (for some obligatory fields) and then the resulting string is created. Version 1.X of HTK depends on HTMLToolkit so it inherit all its functions. On version 2.0, HTK incorporated all HTMLToolkit constructors but with a different approach. As almost all functions differ only in the name of the tag element, now they're just one function, with many closures to make the difference. Also, there are a constructor generator that only build the constructors when they are called, so only the used constructors are really created.

All these changes made version 2.0 at about 60% faster than version 1.0. Besides, the source code is about a third of the previous version, making it easier to maintain, despite its complexity.

what is · download · reference · examples · news · implementation

Created with Vim and Best Viewed on Any Browser
Last modified by Tomás on
Mon Apr 9 12:33:52 2007