The Lua Language
HTK
A library of HTML constructors

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


What is HTK?

HTK is a library of Lua functions that create HTML text. It was designed to be used within CGILua , however HTK does not depend on CGILua.

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: This version is just a reimplementation to make it compatible with all Lua 5.X versions.
You can install it in your LUA_PATH or using LuaRocks . Older versions can be downloaded too:

Reference

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.
htk.BUTTON
htk.COLOR
htk.DATE
htk.DATETIMELOCAL
htk.EMAIL
htk.FILE
htk.HIDDEN
htk.IMAGE
htk.MONTH
htk.NUMBER
htk.PASSWORD
htk.RADIO_BUTTON
htk.RANGE
htk.RESET
htk.SEARCH
htk.SUBMIT
htk.TEL
htk.TEXT
htk.TIME
htk.TOGGLE
htk.URL
htk.WEEK

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 (in HTML, these elements are of tag INPUT, with the TYPE attribute set to "radio").
htk.TOGGLE_LIST creates a group of toggles (in HTML, these elements are of tag INPUT, with the TYPE attribute set to "checkbox").
htk.SELECT creates a selection list (in HTML, this element is of tag SELECT, which should have some (or several) elements of tag OPTION inside it).

Compound Constructors

htk.COMPOUND generic combinator of elements.

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.
htk.TD
htk.TH

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.
htk.DATE_FIELD
htk.FILE_FIELD
htk.MULTILINE_FIELD
htk.PASSWORD_FIELD
htk.RADIO_FIELD
htk.SELECT_FIELD
htk.TEXT_FIELD
htk.TEXTAREA_FIELD
htk.TOGGLE_FIELD

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:
<TR>
<TD class="common">first</TD>
<TD class="special">second</TD>
<TD class="common">third</TD>
</TR>8

Examples

Here are some small examples on 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" })
<B>Bold</B>
Bold
print(htk.BIG {
  separator = "\n",
  "A sample ",
  htk.EM { "text" },
  " with some ",
  htk.B { "formatting tags" },
  htk.BR {},
})
<BIG>
A sample 
<EM>text</EM>
 with some 
<B>formatting tags</B>
<BR>
</BIG>
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>
<TR>
<TD class="common">
Full name
</TD>
<TD class="common">
<INPUT name="name" value="Write your name here" type="text">

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

</TD>
</TR>
</TABLE></FORM>
Full name
Sex Masc.
Fem.
print(htk.BOX {
  separator = '\n',
  htk.SELECT {
    multiple = true,
    name = "cities",
    value = "11",
    options = {
      { "Rio de Janeiro", value="1" },
      { "Friburgo", value="5" },
      { "Saquarema", value="11" },
      { "Cabo Frio", value="12" },
    },
  },
  htk.SELECT {
    multiple = true,
    name = "cities",
    value = "11,1",
    options = {
      { "Rio de Janeiro", value="1" },
      { "Friburgo", value="5" },
      { "Saquarema", value="11" },
      { "Cabo Frio", value="12" },
    },
  },
  htk.SELECT {
    multiple = true,
    name = "cities",
    value = "Friburgo,Cabo Frio",
    options = { "Rio de Janeiro", "Friburgo", "Saquarema", "Cabo Frio", },
  },
  htk.BR {},
  htk.RADIO {
    name = "cities",
    value = "Friburgo",
    options = { "Rio de Janeiro", "Friburgo", "Saquarema", "Cabo Frio", },
  },
})
<SELECT multiple name="cities">
<OPTION name="cities" value="1">Rio de Janeiro</OPTION><BR>
<OPTION name="cities" value="5">Friburgo</OPTION><BR>
<OPTION name="cities" value="11" selected>Saquarema</OPTION><BR>
<OPTION name="cities" value="12">Cabo Frio</OPTION><BR>
</SELECT>
<SELECT multiple name="cities">
<OPTION name="cities" value="1" selected>Rio de Janeiro</OPTION><BR>
<OPTION name="cities" value="5">Friburgo</OPTION><BR>
<OPTION name="cities" value="11" selected>Saquarema</OPTION><BR>
<OPTION name="cities" value="12">Cabo Frio</OPTION><BR>
</SELECT>
<SELECT multiple name="cities">
<OPTION name="cities" value="Rio de Janeiro">Rio de Janeiro</OPTION><BR>
<OPTION name="cities" value="Friburgo" selected>Friburgo</OPTION><BR>
<OPTION name="cities" value="Saquarema">Saquarema</OPTION><BR>
<OPTION name="cities" value="Cabo Frio" selected>Cabo Frio</OPTION><BR>
</SELECT>
<BR>
<INPUT type="radio" name="cities" value="Rio de Janeiro">Rio de Janeiro</INPUT><BR>
<INPUT type="radio" name="cities" value="Friburgo" checked>Friburgo</INPUT><BR>
<INPUT type="radio" name="cities" value="Saquarema">Saquarema</INPUT><BR>
<INPUT type="radio" name="cities" value="Cabo Frio">Cabo Frio</INPUT><BR>


Rio de Janeiro
Friburgo
Saquarema
Cabo Frio
This page was completely generated by HTK -- is its main test! The source code of this page can be downloaded by clicking here

News

Implementation

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
Fri Oct 21 10:41:26 2022