|
HTK |
A library of HTML constructors |
what is
·
download
·
reference
·
examples
·
news
·
implementation
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).
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.
htk.BUTTON
htk.FILE
htk.HIDDEN
htk.PASSWORD
htk.RADIO_BUTTON
htk.RESET
htk.SUBMIT
htk.TEXT
htk.TOGGLE
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.
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
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 source | HTML generated | Final 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 value="Write your name here" type="text" name="name">
</TD>
</TR>
<TR>
<TD class="common">
Sex
</TD>
<TD class="common">
<INPUT value="M" type="radio" name="sex">Masc.</INPUT><BR>
<INPUT value="F" type="radio" name="sex">Fem.</INPUT><BR>
</TD>
</TR>
</TABLE></FORM> | |
This page was completely generated by HTK -- is its main test!
The source code of this page can be downloaded by clicking
here
- 3.1 [09/apr/2007] Deprecated constructions removed
- 3.0.3 [30/may/2006] New version updated to run with Lua 5.1
- 3.0.2 [25/jan/2006] Bug fix: boolean value in attributes
- 3.0.1 [24/jan/2006] Using
module
function - 3.0 [13/out/2003] New version updated to run with Lua 5.0
- 2.0.4 [21/aug/2003] Bug fix (revisited): multiple selected values
- 2.0.3 [25/jul/2002] New feature: default value for class attribute
- 2.0.2 [25/apr/2002] Bug fix: multiple selected values
- 2.0.1 [24/oct/2001] Bug fix: closing tags
- 2.0 [15/oct/2001] Version 2.0 released
- 1.2 [28/sep/2001] Conforming to LTN 007 - Modules & Packages
- [26/mar/2001] Bug fix on Table Rows
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
| and | |
Last modified by Tomás on Mon Apr 9 12:33:52 2007 |